perm filename NOTES.DOC[LSP,JRA]2 blob sn#109167 filedate 1974-07-03 generic text, type T, neo UTF8
␈↓␈↓↓␈↓ 	sCONTENTS     i␈↓


␈↓↓␈↓ ∧ZT A B L E   O F   C O N T E N T S



␈↓↓SECTION␈↓ ¬PAGE␈↓


␈↓␈↓ ↓x1␈↓ α8LISP - A HIGH-LEVEL MATHEMATICAL LANGUAGE FOR DATA
␈↓␈↓ ¬(STRUCTURES␈↓ I1


␈↓␈↓ ↓x2␈↓ α8SYMBOLIC EXPRESSIONS␈↓ I4

␈↓␈↓ βλ2.1␈↓ βhBinary trees␈↓ ¬_∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ I5
␈↓␈↓ βλ2.2␈↓ βhProblems␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ I7
␈↓␈↓ βλ2.3␈↓ βhPrimitive Functions␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ I7
␈↓␈↓ βλ2.4␈↓ βhConditional Expressions␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :10
␈↓␈↓ βλ2.5␈↓ βhProblems␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :13
␈↓␈↓ βλ2.6␈↓ βhList Notation␈↓ ¬_∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :14
␈↓␈↓ βλ2.7␈↓ βhProblems involving list-notation␈↓ πλ∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :15
␈↓␈↓ βλ2.8␈↓ βh␈↓αlist␈↓ and ␈↓αnull␈↓␈↓ ¬_∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :16
␈↓␈↓ βλ2.9␈↓ βhA respite␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :16


␈↓␈↓ ↓x3␈↓ α8APPLICATIONS AND EXAMPLES OF LISP DEFINITIONS␈↓ :18

␈↓␈↓ βλ3.1␈↓ βhIntroduction␈↓ ¬_∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :18
␈↓␈↓ βλ3.2␈↓ βhExamples of definitions␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :19
␈↓␈↓ βλ3.3␈↓ βhProblems␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :23
␈↓␈↓ βλ3.4␈↓ βhExamples of LISP applications␈↓ πλ∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :24
␈↓␈↓ βλ3.5␈↓ βhDifferentiation␈↓ ¬H∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :25
␈↓␈↓ βλ3.6␈↓ βhAlgebra of polynomials␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :31
␈↓␈↓ βλ3.7␈↓ βhEvaluation of polynomials␈↓ εX∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :35
␈↓␈↓ βλ3.8␈↓ βhProblems␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :35
␈↓␈↓ βλ3.9␈↓ βhThe great mothers␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :37
␈↓␈↓ βλ3.10␈↓ βhProving properties of programs␈↓ πλ∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :39


␈↓␈↓ ↓x4␈↓ α8EVALUATION OF LISP EXPRESSIONS␈↓ :41

␈↓␈↓ βλ4.1␈↓ βhIntroduction␈↓ ¬_∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :41
␈↓␈↓ βλ4.2␈↓ βhSexpr translation of LISP expressions␈↓ πh∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :43
␈↓␈↓ βλ4.3␈↓ βhA first look at symbol tables␈↓ εX∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :45
␈↓␈↓ βλ4.4␈↓ βhIntroduction to ␈↓αλ␈↓-expressions␈↓ εX∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :46
␈↓␈↓ βλ4.5␈↓ βhReview of evaluation␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :46
␈↓␈↓ βλ4.6␈↓ βh␈↓αλ␈↓-expressions␈↓ ¬_∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :47
␈↓␈↓ βλ4.7␈↓ βhMechanization of evaluation␈↓ εX∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :49
␈↓␈↓ βλ4.8␈↓ βhExamples of ␈↓αeval␈↓␈↓ ¬H∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :52
␈↓␈↓↓ii  CONTENTS␈↓ X␈↓


␈↓␈↓ βλ4.9␈↓ βhEnvironments␈↓ ¬_∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :57
␈↓␈↓ βλ4.10␈↓ βhHacking with ␈↓αeval␈↓ and friends.␈↓ πλ∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :59
␈↓␈↓ βλ4.11␈↓ βh␈↓αlabel␈↓␈↓ ∧8∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :59
␈↓␈↓ βλ4.12␈↓ βhfunctional arguments␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :60
␈↓␈↓ βλ4.13␈↓ βhspecial forms␈↓ ¬_∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :64
␈↓␈↓ βλ4.14␈↓ βhThe ␈↓αprog␈↓-feature␈↓ ¬H∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :66
␈↓␈↓ βλ4.15␈↓ βhProblems involving ␈↓αprog␈↓␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :69
␈↓␈↓ βλ4.16␈↓ βhIn retrospect␈↓ ¬_∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :70


␈↓␈↓ ↓x5␈↓ α8RUNNING ON THE MACHINE␈↓ :72

␈↓␈↓ βλ5.1␈↓ βh␈↓αevalquote␈↓␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :73
␈↓␈↓ βλ5.2␈↓ βhA project: extensions to ␈↓αeval␈↓␈↓ εX∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :75
␈↓␈↓ βλ5.3␈↓ βhA project: Syntax-directed processes␈↓ π8∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :78
␈↓␈↓ βλ5.4␈↓ βhA project: Syntax-directed I/O␈↓ πλ∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :82


␈↓␈↓ ↓x6␈↓ α8TOWARD BETTER SYMBOL TABLES AND IMPLEMENTATION␈↓ :87

␈↓␈↓ βλ6.1␈↓ βhA picture of the atom ␈↓αNIL␈↓␈↓ εX∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :93
␈↓␈↓ βλ6.2␈↓ βhA first look at ␈↓αcons␈↓␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :94
␈↓␈↓ βλ6.3␈↓ βhGarbage collection␈↓ ¬H∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :94
␈↓␈↓ βλ6.4␈↓ βh␈↓αread␈↓ and ␈↓αprint␈↓␈↓ ¬_∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :96
␈↓␈↓ βλ6.5␈↓ βhHashing␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ :98
␈↓␈↓ βλ6.6␈↓ βhA review of the structure of  the LISP machine␈↓ λH∀∀∀∀∀∀∀␈↓ α⊗␈↓ +101
␈↓␈↓ βλ6.7␈↓ βhMore on symbol tables␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +102
␈↓␈↓ βλ6.8␈↓ βhGlobal Variables␈↓ ¬H∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +102
␈↓␈↓ βλ6.9␈↓ βhAMBIT/G␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +103
␈↓␈↓ βλ6.10␈↓ βhgarbage collection again␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +109
␈↓␈↓ βλ6.11␈↓ βhSyntactic dominoes␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +117
␈↓␈↓ βλ6.12␈↓ βhThe Contour Model␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +117
␈↓␈↓ βλ6.13␈↓ βhMacros and special forms␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +119
␈↓␈↓ βλ6.14␈↓ βhProblems␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +122
␈↓␈↓ βλ6.15␈↓ βh␈↓	SM␈↓-␈↓αeval␈↓␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +122
␈↓␈↓ βλ6.16␈↓ βhBinding revisited␈↓ ¬H∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +124
␈↓␈↓ βλ6.17␈↓ βhReview␈↓ ∧8∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +127
␈↓␈↓ βλ6.18␈↓ βh␈↓	SM␈↓:A Simple machine␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +128
␈↓␈↓ βλ6.19␈↓ βhAn alternative: deep bindings␈↓ πλ∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +130


␈↓␈↓ ↓x7␈↓ α8COMPILATION␈↓ +132

␈↓␈↓ βλ7.1␈↓ βhOn LISP and Semantics␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +132
␈↓␈↓ βλ7.2␈↓ βhMore on Compilation␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +136
␈↓␈↓ βλ7.3␈↓ βhCompilers for subsets of LISP␈↓ πλ∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +138
␈↓␈↓ βλ7.4␈↓ βhProblems␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +142
␈↓␈↓ βλ7.5␈↓ βhOne-pass Assemblers␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +142
␈↓␈↓↓␈↓ 	aCONTENTS     iii␈↓


␈↓␈↓ βλ7.6␈↓ βhA compiler for simple ␈↓αeval␈↓␈↓ εX∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +147
␈↓␈↓ βλ7.7␈↓ βhA project: Efficient compilation␈↓ πλ∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +153
␈↓␈↓ βλ7.8␈↓ βhA project: One-pass assembler␈↓ πλ∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +154
␈↓␈↓ βλ7.9␈↓ βhA project: Syntax directed compilation␈↓ πh∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +155
␈↓␈↓ βλ7.10␈↓ βhA deep-binding compiler␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +155
␈↓␈↓ βλ7.11␈↓ βhCompilation and global variables␈↓ π8∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +155
␈↓␈↓ βλ7.12␈↓ βhFunctional Arguments␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +157
␈↓␈↓ βλ7.13␈↓ βhTracing and debugging in LISP␈↓ πλ∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +157
␈↓␈↓ βλ7.14␈↓ βhMacros and special forms␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +158
␈↓␈↓ βλ7.15␈↓ βhNumbers␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +159


␈↓␈↓ ↓x8␈↓ α8STORAGE STRUCTURES AND EFFICIENCY␈↓ +162

␈↓␈↓ βλ8.1␈↓ βhBit-tables␈↓ ∧h∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +162
␈↓␈↓ βλ8.2␈↓ βhVectors and arrays␈↓ ¬x∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +163
␈↓␈↓ βλ8.3␈↓ βhstrings and linear LISP␈↓ ε(∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +166
␈↓␈↓ βλ8.4␈↓ βh␈↓αrplaca␈↓ and ␈↓αrplacd␈↓␈↓ ¬H∀∀∀∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +168
␈↓␈↓ βλ8.5␈↓ βhApplications of ␈↓αrplaca␈↓ and ␈↓αrplacd␈↓␈↓ π8∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +169


␈↓␈↓ ↓x9␈↓ α8SYSTEMS ORGANIZATION␈↓ +175

␈↓␈↓ βλ9.1␈↓ βhTime-sharing organization␈↓ εX∀∀∀∀∀∀∀∀∀∀∀∀␈↓ α⊗␈↓ +178


␈↓␈↓ ↓x10␈↓ α8IMPLICATIONS FOR OTHER LANGUAGES␈↓ +181


␈↓␈↓ ↓x11␈↓ α8BIBLIOGRAPHY␈↓ +182
␈↓␈↓↓1.␈↓ 	mIntroduction     1␈↓


␈↓␈↓ ε␈↓↓SECTION 1

␈↓↓␈↓ απLISP - A HIGH-LEVEL MATHEMATICAL LANGUAGE FOR DATA STRUCTURES␈↓




␈↓␈↓ ∧λ"...␈α∞it␈α∂is␈α∞important␈α∂not␈α∞to␈α∞lose␈α∂sight␈α∞of␈α∂the␈α∞fact␈α∞that␈α∂there␈α∞is␈α∂a␈α∞difference
␈↓␈↓ ∧λbetween␈α⊃training␈α⊃and␈α⊃education.␈α∩ If␈α⊃computer␈α⊃science␈α⊃is␈α∩a␈α⊃fundamental
␈↓␈↓ ∧λdiscipline,␈α_then␈α↔university␈α_education␈α_in␈α↔this␈α_field␈α_should␈α↔emphasize
␈↓␈↓ ∧λenduring␈α
fundamental␈α
principles␈α
rather␈α
than␈α
transient␈α
current␈α
technology."

␈↓␈↓ πTPeter Wegner, ␈↓αThree Computer Cultures␈↓ 

␈↓Just␈α∩as␈α∩it␈α∩is␈α∩unnecessary␈α∩to␈α∩learn␈α⊃machine␈α∩language␈α∩to␈α∩study␈α∩numerical␈α∩algorithms,␈α∩it␈α∩is␈α⊃also
␈↓unnecessary␈αto␈αlearn␈αmachine␈αlanguage␈αto␈αunderstand␈αnon-numerical␈αprocesses.␈α The␈αdistinction␈αwe
␈↓are␈α∃making␈α∃is␈α∃between␈α∀data␈α∃structures␈α∃and␈α∃storage␈α∀structures.␈α∃ That␈α∃is,␈α∃data␈α∃structures␈α∀are
␈↓independent␈α∞of␈α∞␈↓¬how␈↓␈α∞they␈α∞are␈α∞implemented␈α∂on␈α∞a␈α∞machine.␈α∞ Data␈α∞structures␈α∞are␈α∂representations␈α∞of
␈↓information␈α∞chosen␈α∞to␈α
exhibit␈α∞certain␈α∞ordering␈α∞and␈α
accessibility␈α∞relationships␈α∞between␈α∞data␈α
items.
␈↓Certainly␈α
in␈α
the␈α
real␈α
world␈α
you␈α
cannot␈α
ignore␈α
storage␈α
structures␈α
when␈α
you␈α
are␈α
deciding␈α
upon␈α
the
␈↓data␈α∂structures␈α∂which␈α∂will␈α∂encode␈α∂your␈α⊂problem,␈α∂but␈α∂the␈α∂interesting␈α∂aspects␈α∂of␈α⊂representation␈α∂of
␈↓information␈α∩can␈α∩be␈α∩discussed␈α∩at␈α∩the␈α∩level␈α⊃of␈α∩data␈α∩structures␈α∩with␈α∩no␈α∩loss␈α∩of␈α∩generality.␈α⊃ The
␈↓mapping␈α⊂of␈α⊂data␈α⊂structures␈α⊂to␈α⊂storage␈α⊂structures␈α⊂is␈α⊂machine␈α⊂dependent␈α⊂anyway,␈α⊂and␈α⊂consists␈α∂of
␈↓bit-pushing,␈α
trickery␈α
and␈α
black␈α
magic.␈α
 We␈α
are␈α
more␈α
interested␈α
in␈α
ideas␈α
than␈α
coding␈α
tricks.

␈↓A␈α→very␈α_crucial␈α→problem␈α→in␈α_data␈α→structures␈α_and␈α→algorithms␈α→is␈α_the␈α→interplay␈α→between␈α_the
␈↓representation␈α∪and␈α∩the␈α∪algorithm:␈α∩if␈α∪you␈α∪pick␈α∩a␈α∪frugal␈α∩representation␈α∪perhaps␈α∪your␈α∩accessing
␈↓algorithms␈αbecome␈αmore␈αtime␈αconsuming␈αor␈αthe␈αalgorithm␈αbecome␈αless␈αgeneral.␈α We␈αwill␈αstudy␈αthis
␈↓interrelation␈α⊃carefully␈α⊃in␈α⊂this␈α⊃text.␈α⊃We␈α⊂will␈α⊃also␈α⊃see␈α⊂that␈α⊃it␈α⊃is␈α⊂possible,␈α⊃and␈α⊃most␈α⊃beneficial,␈α⊂to
␈↓structure␈αour␈αprograms␈α
such␈αthat␈αthere␈αis␈α
a␈αvery␈αclean␈αinterface␈α
between␈αthe␈αabstract␈αalgorithm␈α
and
␈↓the␈αchosen␈αrepresentation.␈α That␈αis,␈αthere␈α
will␈αbe␈αa␈αset␈αof␈αrepresentation-manipulating␈α
programs␈αto
␈↓test,␈α
select␈α
or␈α
construct␈α
elements␈αof␈α
the␈α
domain;␈α
and␈α
there␈αwill␈α
be␈α
a␈α
program␈α
encoding␈αthe␈α
algorithm.
␈↓To␈αchange␈αrepresentations␈αonly␈αrequires␈αchanges␈αto␈αconstructors,␈αselectors␈αand␈αpredicates,␈αnot␈αto␈α
the
␈↓basic␈α
program.

␈↓However,␈αthis␈αtext␈αis␈α␈↓↓not␈↓␈αmeant␈αto␈αbe␈αa␈αprogramming␈αmanual␈αfor␈αLISP.␈α A␈αcertain␈αamount␈αof␈αtime
␈↓is␈αspent␈αgiving␈α
insights␈αinto␈αtechniques␈αfor␈α
writing␈αLISP␈αfunctions.␈α There␈α
are␈αtwo␈αreasons␈αfor␈α
this.
␈↓First,␈α∩the␈α∩style␈α∩of␈α∪LISP␈α∩programming␈α∩is␈α∩quite␈α∪different␈α∩from␈α∩that␈α∩of␈α∪"normal"␈α∩programming.
␈↓Second,␈α∩and␈α⊃more␈α∩important,␈α∩we␈α⊃will␈α∩spend␈α⊃a␈α∩great␈α∩deal␈α⊃of␈α∩time␈α⊃discussing␈α∩various␈α∩levels␈α⊃of
␈↓implementation␈αof␈αthe␈αlanguage.␈αLISP␈αis␈αan␈αexcellent␈αmedium␈αfor␈αintroducing␈αstandard␈αtechniques
␈↓such␈α∞as␈α
recursion,␈α∞garbage␈α∞collection,␈α
storage␈α∞allocation,␈α∞and␈α
hashing.␈α∞It␈α∞is␈α
pointless␈α∞to␈α∞attempt␈α
to
␈↓discuss␈α
implementation␈α
of␈α
a␈α
language␈α
which␈α
is␈α
poorly␈α
understood.

␈↓If␈α⊃the␈α⊃only␈α⊃reasons␈α⊂for␈α⊃learning␈α⊃a␈α⊃programming␈α⊃language␈α⊂were␈α⊃its␈α⊃applications,␈α⊃then␈α⊃for␈α⊂most
␈↓standard␈α
applications␈α
we␈αcould␈α
do␈α
better␈αthan␈α
LISP.␈α
 LISP␈αis␈α
at␈α
least␈αfifteen␈α
years␈α
old␈α
and␈αmany
␈↓␈↓↓2  Introduction␈↓ E1.␈↓


␈↓languages␈α
now␈α
offer␈α
themselves␈α
with␈α
better␈α
notation,␈α
more␈α
efficient␈α
running␈α
code,␈α
or␈αlarger␈α
varieties
␈↓of␈α
data␈α
structure.␈α
 But␈α∞the␈α
more␈α
interesting␈α
aspects␈α
of␈α∞LISP␈α
for␈α
students␈α
of␈α
Computer␈α∞Science␈α
lie
␈↓not␈α⊂in␈α⊂its␈α⊃features␈α⊂as␈α⊂a␈α⊃programming␈α⊂language,␈α⊂but␈α⊂in␈α⊃what␈α⊂it␈α⊂can␈α⊃show␈α⊂about␈α⊂the␈α⊃about␈α⊂the
␈↓␈↓↓structure␈↓␈α⊃of␈α⊃Computer␈α⊃Science.␈α⊃The␈α∩application␈α⊃of␈α⊃computers,␈α⊃as␈α⊃is␈α⊃hardware␈α∩development,␈α⊃is
␈↓peripheral␈α⊂to␈α∂this␈α⊂field.␈α∂There␈α⊂is␈α∂a␈α⊂rapidly␈α∂expanding␈α⊂body␈α∂of␈α⊂knowledge␈α∂unique␈α⊂to␈α∂Computer
␈↓Science,␈αneither␈αmathematical␈α
nor␈αengineering␈αper␈α
se.␈α Much␈αof␈αthis␈α
area␈αis␈αpresented␈α
most␈αclearly
␈↓by␈α∩studying␈α∩LISP.␈α∪ As␈α∩mentioned␈α∩above,␈α∩language␈α∪implementors␈α∩can␈α∩learn␈α∩much␈α∪from␈α∩LISP.
␈↓Indeed,␈α⊃many␈α⊃of␈α⊃the␈α⊃"standard␈α⊃techniques"␈α⊃in␈α⊃language␈α⊃implementation␈α⊃(e.g.␈α⊃built-in␈α⊃recursion,
␈↓garbage␈αcollection)␈αoriginated␈αwith␈αLISP.␈αMany␈αof␈αthe␈αmore␈αtheoretical␈αareas␈αof␈αComputer␈αScience
␈↓(e.g.␈α↔provability,␈α_semantics)␈α↔can␈α↔be␈α_introduced␈α↔using␈α↔LISP.␈α_Many␈α↔of␈α↔the␈α_most␈α↔interesting
␈↓applications␈α
of␈α
computers␈α
(e.g.␈α
artifical␈α
intelligence,␈α
game␈α
playing,␈α
theorem␈α
proving)␈α
have␈α
been␈α
done
␈↓using␈α
LISP.

␈↓Much␈α
of␈α
the␈α
power␈αof␈α
LISP␈α
lies␈α
in␈αits␈α
simplicity.␈α
It␈α
is␈αa"fixed␈α
point"␈α
in␈α
non-numerical␈αlanguages.
␈↓The␈α
data␈αstructures␈α
are␈αrich␈α
enough␈αto␈α
easily␈α
describe␈αsophisticated␈α
algorithms␈αbut␈α
not␈αso␈α
rich␈αas␈α
to
␈↓become␈α↔obfuscatory.␈α↔ We␈α↔will␈α↔describe␈α↔translators␈α↔--␈α↔interpreters␈α↔and␈α↔compilers␈α↔--␈α↔as␈α⊗very
␈↓transparent␈α
LISP␈α
functions.␈α
 Indeed␈α
the␈αstructure␈α
of␈α
the␈α
compilation␈α
process␈α
becomes␈αso␈α
transparent
␈↓as␈αto␈αborder␈αon␈αthe␈αtrivial.␈α This␈αis␈αnot␈αto␈αsay␈αthat␈αcompiler␈αwriting␈αis␈αtrivial␈αor␈αeven␈αeasy;␈αbut␈αthe
␈↓structure␈α∂of␈α∞the␈α∂process␈α∞is␈α∂easily␈α∂seen.␈α∞ This␈α∂is␈α∞partly␈α∂due␈α∂to␈α∞the␈α∂simplicity␈α∞of␈α∂the␈α∂language,␈α∞but
␈↓perhaps␈α∂more␈α∂due␈α∂to␈α∂our␈α∂ability␈α∂to␈α∞go␈α∂right␈α∂to␈α∂the␈α∂compiling␈α∂process␈α∂without␈α∂becoming␈α∞bogged
␈↓down␈αin␈αdetails␈αof␈αscanners␈αand␈αparsers␈α--␈αsyntax--.␈αAgain,␈αsyntax␈αanalysis␈αis␈αnot␈αtrivial,␈αbut␈αit␈αis␈αa
␈↓peripheral␈α
activity␈α
to␈α
our␈α
understanding␈α
of␈α
compilers␈α
and␈α
evaluators.

␈↓LISP␈α∩is␈α∩a␈α∩better␈α∩vehicle␈α∩than␈α∩its␈α∩ancestor,␈α∩the␈α∩λ-calculus,␈α∩because␈α∩there␈α∩is␈α∩just␈α∪enough␈α∩more
␈↓complexity␈α
present␈αin␈α
LISP␈αto␈α
make␈α
its␈αimplementation␈α
a␈αrealistic␈α
Computer␈αScience␈α
task␈α
and␈αnot
␈↓just␈αan␈αinteresting␈α
mathematical␈αcuriosity.␈αMost␈αevery␈α
aspect␈αof␈αthe␈α
implementation␈αof␈αLISP␈αand␈α
its
␈↓translators␈αhas␈αimmediate␈αimplications␈αto␈αthe␈αimplementation␈αof␈αother␈αlanguages␈αand␈αto␈αthe␈αdesign
␈↓of␈α
programming␈α
languages␈α
in␈α
general.

␈↓LISP␈α⊃has␈α∩very␈α⊃important␈α∩implications␈α⊃in␈α∩the␈α⊃field␈α∩of␈α⊃programming␈α∩language␈α⊃semantics␈α∩--␈α⊃the
␈↓meaning␈α∩of␈α∩languages--,␈α∩also␈α∩in␈α∩a␈α∩field␈α∩of␈α∩much␈α∩current␈α∩interest,␈α∩provability␈α∩of␈α∩properties␈α∩of
␈↓programs.␈α⊂ These␈α⊂notes␈α⊂are␈α⊂primarily␈α⊂designed␈α⊂for␈α⊂undergraduates␈α⊂and␈α⊂therefore␈α⊂an␈α⊃attempt␈α⊂is
␈↓made␈αto␈αmake␈αthem␈αself-contained.␈αThere␈αare␈αbasically␈αfour␈αareas␈αin␈αwhich␈αto␈αdistribute␈αthe␈αtopics
␈↓covered:␈αthe␈αmechanics␈αof␈αthe␈αlanguage,␈αthe␈αevaluation␈αof␈αexpressions␈αin␈αLISP,␈αthe␈αimplementation
␈↓of␈α
LISP,␈α
and␈α
the␈α
compilers␈α
for␈α
LISP.␈α
Each␈α
area␈α
builds␈α
on␈α
the␈α
previous.␈α
Taken␈α
as␈α
a␈α
group␈α
these
␈↓topics␈αintroduce␈αmuch␈αof␈α
what␈αis␈αinteresting␈αComputer␈αScience.␈α
 The␈αsooner␈αLISP␈αis␈α
mastered␈αthe
␈↓better.

␈↓A␈α
large␈αcollection␈α
of␈α
problems␈αhas␈α
been␈α
included.␈αThe␈α
reader␈α
is␈αurged␈α
to␈α
do␈αas␈α
many␈α
as␈αpossible.
␈↓The␈αproblems␈α
are␈αmostly␈α
non-trivial;␈αthey␈α
attempt␈αto␈α
be␈αrealistic,␈α
introducing␈αsome␈αnew␈α
information
␈↓which␈α
the␈α
readers␈α
should␈α
be␈α
able␈α
to␈α
discover␈α
themselves.

␈↓There␈α∞are␈α∂also␈α∞a␈α∂few␈α∞rather␈α∂substantial␈α∞projects.␈α∞ At␈α∂least␈α∞one␈α∂should␈α∞be␈α∂attempted.␈α∞ There␈α∂is␈α∞a
␈↓significant␈α
difference␈α
between␈α
being␈α
able␈α
to␈α
program␈α
small␈α
problems␈α
and␈α
handle␈α
large␈α
projects.␈α
The
␈↓increase␈α
in␈α
difficulty␈α
is␈α
exponential␈α
rather␈α
than␈α
linear.
␈↓␈↓↓1.␈↓ 	mIntroduction     3␈↓


␈↓If␈α
you␈α
take␈αa␈α
course␈α
in␈αelementary␈α
number␈α
theory␈αor␈α
better␈α
yet␈αrecursive␈α
function␈α
theory,␈αyou␈α
would
␈↓begin␈αwith␈αa␈αprecise␈αdescription␈αof␈αthe␈αdomain␈αof␈αinterest␈α(the␈αnon-negative␈αintegers␈αperhaps,␈αor␈α0
␈↓and␈α∞the␈α∞successor␈α∞function)␈α∞and␈α∞then␈α∞describe␈α∂the␈α∞class␈α∞of␈α∞functions␈α∞or␈α∞operations␈α∞which␈α∂will␈α∞be
␈↓allowed␈α
in␈α
the␈α
developing␈α
theory.␈α
We␈α
will␈α
do␈α
the␈α
same.

␈↓We␈αwill␈αsee␈α
other␈αparallels␈αbetween␈α
LISP␈αand␈αelementary␈αnumber␈α
theory.␈α For␈αexample,␈α
the␈αproof
␈↓techniques␈α⊗used␈α⊗in␈α∃LISP␈α⊗are␈α⊗generalizations␈α∃of␈α⊗those␈α⊗of␈α∃elementary␈α⊗number␈α⊗theory.␈α∃ Most
␈↓important,␈α∂there␈α∞is␈α∂the␈α∞same␈α∂subtle␈α∂increase␈α∞in␈α∂power␈α∞of␈α∂expression␈α∞and␈α∂complexity.␈α∂The␈α∞simple
␈↓beginnings␈α
of␈α
both␈α
theories␈α
lead␈α
rapidly␈α
to␈α
non-trivial␈α
discussions.
␈↓␈↓↓4  Symbolic expressions␈↓ C2.␈↓


␈↓␈↓ ¬␈␈↓↓SECTION 2

␈↓↓␈↓ ¬∂SYMBOLIC EXPRESSIONS␈↓




␈↓Our␈αprimitive␈αdomain␈αconsists␈αof␈αobjects␈α
called␈αatoms␈αor␈αatomic␈αsymbols.␈α Atoms␈αcontain␈α
the␈αclass
␈↓usually␈α
called␈α
literal␈α
atoms␈α
and␈α
the␈α
class␈α
of␈α
signed␈α
integers;␈α
i.e.:


␈↓<atom>␈↓ β(:: = <literal atom>|<number>| -<number>
␈↓<literal atom>␈↓ β(:: = <atom letter>|<literal atom><atom letter>|<literal atom><digit>
␈↓<number>␈↓ β(:: = <digit>|<number><digit>
␈↓<atom letter>␈↓ β(:: =␈↓α A |B |C ...| Z␈↓
␈↓<digit>␈↓ β(:: = ␈↓α0 |1 |2 ... |9␈↓


␈↓For example:␈↓ βxatoms␈↓ ¬hnot atoms
␈↓␈↓α␈↓ βxABC123␈↓ ¬h2a
␈↓α␈↓ βx12␈↓ ¬ha
␈↓α␈↓ βxA4D6␈↓ ¬h$$g
␈↓α␈↓ βxNIL␈↓ ¬hABD.
␈↓α␈↓ βxT␈↓ ¬h(A . B)


␈↓Most␈α
of␈α
our␈α
discussions␈α
will␈α
deal␈α
with␈αnon-numeric␈α
atoms.␈α
From␈α
the␈α
point␈α
of␈α
view␈α
of␈αdata␈α
structure,
␈↓numbers␈α⊃are␈α∩not␈α⊃very␈α∩interesting.␈α⊃Most␈α∩implementations␈α⊃of␈α∩LISP␈α⊃do␈α∩however␈α⊃contain␈α∩a␈α⊃large
␈↓arithmetic␈αentourage,␈αincluding␈αfloating␈αpoint␈αnumbers␈αand␈αoperations.␈αMany␈αimplementations␈αalso
␈↓give␈α∂a␈α⊂wider␈α∂class␈α⊂of␈α∂literal␈α⊂atoms,␈α∂allowing␈α∂some␈α⊂special␈α∂characters␈α⊂to␈α∂appear;␈α⊂for␈α∂most␈α⊂of␈α∂our
␈↓discussion␈α
the␈α
above␈α
class␈α
is␈α
quite␈α
sufficient.

␈↓Using␈αan␈αoperator␈αto␈αbe␈αdescribed␈αlater,␈αwe␈αare␈αable␈αto␈αenlarge␈αon␈αthis␈αprimitive␈αdomain␈αobtaining
␈↓the␈α⊗domain␈α↔of␈α⊗interest␈α↔for␈α⊗LISP␈α↔functions,␈α⊗that␈α⊗is␈α↔the␈α⊗class␈α↔of␈α⊗␈↓¬symbolic␈α↔expressions␈↓␈α⊗or
␈↓␈↓¬S-expressions␈↓␈α
or␈α
also␈αcalled␈α
␈↓¬S-exprs␈↓.␈α
 S-exprs␈α
include␈αas␈α
a␈α
proper␈α
subset␈αthe␈α
atoms,␈α
but␈αS-exprs␈α
can
␈↓be␈αconstructed␈α
of␈αother␈α
S-exprs␈αas␈α
follows:␈αa␈α
left-paren␈αfollowed␈α
by␈αan␈α
S-expr,␈αfollowed␈α
by␈αa␈α
period,
␈↓followed␈α
by␈α
an␈α∞S-expr,␈α
followed␈α
by␈α∞a␈α
right-paren␈α
is␈α
also␈α∞an␈α
S-expr.␈α
 Non-atomic␈α∞sexpressions␈α
are
␈↓also␈α
called␈α
␈↓↓dotted-pairs␈↓.␈α
 Here's␈α
a␈α
BNF␈α
description␈α
of␈α
S-expressions.

␈↓␈↓ ∧O<sexpr> :: = <atom>|␈↓α(␈↓<sexpr> . <sexpr>␈↓α) 

␈↓Notice␈α∂that␈α∂if␈α∂we␈α∂allow␈α∂floating␈α∂point␈α∂numbers␈α∂as␈α∂atoms␈α∂some␈α∂care␈α∂needs␈α∂to␈α∂be␈α∂exercised␈α∞when
␈↓writing␈αsexpressions.␈αHow␈αshould␈α␈↓α(A.1.2)␈↓␈αbe␈αinterpreted.␈α Is␈αit␈αthe␈αdotted␈αpair␈α␈↓α(A␈α.␈α1.2)␈↓,␈αor␈αis␈αit␈αjust
␈↓an␈α→ill-formed␈α~expression?␈α→ Evaluation␈α→of␈α~such␈α→ambiguous␈α→constructs␈α~will␈α→depend␈α~on␈α→the
␈↓implementation;␈α
such␈α
details␈α
do␈α
not␈α
interest␈α
us␈α
yet.
␈↓␈↓↓2.␈↓ λuSymbolic expressions     5␈↓


␈↓Examples:␈↓ βxS-exprs␈↓ ε8not S-exprs
␈↓α␈↓ βxA␈↓ ε8A . B
␈↓α␈↓ βx(A . B)␈↓ ε8(A . B . C)
␈↓α␈↓ βx(((A.B) .C) . (A.B))␈↓ ε8((A . B)))



␈↓␈↓ ¬d␈↓↓2.1  Binary trees␈↓


␈↓S-exprs␈α⊂have␈α⊃a␈α⊂natural␈α⊂interpretation␈α⊃as␈α⊂␈↓¬binary␈α⊂trees␈↓.␈α⊃ A␈α⊂binary␈α⊂tree␈α⊃is␈α⊂a␈α⊃branching␈α⊂structure
␈↓consisting␈α
of␈α
a␈αsingle␈α
root␈α
node␈α
and␈αperhaps␈α
a␈α
collection␈α
of␈αterminal␈α
and␈α
non-terminal␈αnodes.␈α
 From
␈↓non-terminal␈α⊂nodes␈α⊃we␈α⊂sprout␈α⊂exactly␈α⊃two␈α⊂branches;␈α⊃no␈α⊂branches␈α⊂are␈α⊃grown␈α⊂from␈α⊃the␈α⊂terminal
␈↓nodes.␈α
 And␈αmost␈α
important:␈α
there␈αare␈α
no␈α
intersecting␈αbranches.␈α
 We␈α
will␈αtalk␈α
about␈α
more␈αgeneral
␈↓structures␈α⊂later␈α∂(called␈α⊂list-structures␈α∂or␈α⊂directed␈α∂graphs).␈α⊂ In␈α∂our␈α⊂discussions,␈α∂an␈α⊂<atom>␈α⊂will␈α∂be
␈↓attached␈α
to␈α
each␈α
terminal␈α
node,␈α
and␈α
non-terminal␈α
nodes␈α
will␈α
be␈α
unlabeled.


␈↓Here␈α
are␈α
some␈α
binary␈α
trees:

␈↓α                                        .








␈↓α␈↓ αλ1    2       A   D   E␈↓ ε8A    B    NIL


␈↓Perhaps␈α
you␈αcan␈α
see␈αhow␈α
to␈αinterpret␈α
S-exprs␈αnow.␈α
 The␈αatoms␈α
are␈αinterpreted␈α
as␈α
terminal␈αnodes;
␈↓and␈α
since␈α
non-atomic␈α
S-exprs␈α
always␈α
have␈αtwo␈α
sub-expressions␈α
we␈α
can␈α
write␈α
the␈αfirst␈α
sub-expression
␈↓as␈α
the␈α
left␈α
branch␈α
of␈α
a␈α
binary␈α
tree␈α
and␈α
the␈α
second␈α
sub-␈α
expression␈α
as␈α
the␈α
right␈α
branch.
␈↓␈↓↓6  Symbolic expressions␈↓ 72.1␈↓



␈↓For␈α
example:

␈↓α␈↓ αX(A . B)␈↓ ε8(A . (B . C))





␈↓α␈↓ αXA   B␈↓ ε8A    B    C



␈↓α␈↓or:␈↓α

␈↓α␈↓ αX((A . B) . C)␈↓ ε8(A . (B . NIL))









␈↓α␈↓ αXA     B         C␈↓ ε8 A     B     NIL

␈↓Other␈α⊗representations␈α⊗of␈α⊗binary␈α⊗trees␈α⊗which␈α⊗will␈α⊗be␈α⊗more␈α⊗suggestive␈α⊗when␈α⊗we␈α⊗talk␈α∃about
␈↓implementation␈α
of␈α
LISP␈α
are:␈α
␈↓α




␈↓α(A .(B . C)) ␈↓is:␈↓α       A                                ␈↓or␈↓α    A            B         C



␈↓α                              B    C
␈↓␈↓↓2.2␈↓ 
~Problems     7␈↓


␈↓␈↓ ¬v␈↓↓2.2  Problems␈↓

␈↓I Which of the following are dotted-pairs?
␈↓␈↓ αX␈↓↓1.␈↓α (X . Y)   ␈↓↓2.␈↓α ((A .(B . C))  ␈↓↓3.␈↓α  A2   ␈↓↓4.␈↓α (X . Y2 . Z)


␈↓II Write the following as binary trees:
␈↓␈↓ αX␈↓↓1.␈↓α  ((A . B).(B . (C . D)))  ␈↓↓2.␈↓α  (((A . B).C).E)

␈↓α␈↓ αX␈↓↓3.␈↓α  ((X . NIL).(Y .(Z . NIL)))    ␈↓↓4.␈↓α  (NIL . NIL)


␈↓III Write the following binary trees as Sexprs:

␈↓␈↓ αX␈↓↓1.              2.                       3.
␈↓↓␈↓ αX␈↓α                    A
␈↓α␈↓ αX  A
␈↓α␈↓ αX     B  C                                  A



␈↓α␈↓ αX                                                 B

␈↓α␈↓ αX                C   NIL       D    E

␈↓α␈↓ αX                                                    C  NIL



␈↓↓␈↓ αX4.                                5.
␈↓α␈↓ αX    CAR            NIL

␈↓α␈↓ αX                                               CONS        X         Y  NIL


␈↓α␈↓ αX                    QUOTE         A    NIL




␈↓␈↓ ¬-␈↓↓2.3  Primitive Functions␈↓


␈↓So␈αmuch␈αfor␈αthe␈αdomain␈αof␈αobjects;␈αwhat␈αwe␈αneed␈αnow␈αare␈αsome␈αfunctions␈αor␈αoperators␈αto␈αperform
␈↓operations␈αon␈αthe␈αdomain.␈α The␈αfirst␈αsuch␈αfunction␈αis␈αthe␈α␈↓αcons␈↓␈α(construct)␈αfunction␈αwhich␈αis␈αused␈αto
␈↓␈↓↓8  Symbolic expressions␈↓ 52.3␈↓


␈↓generate␈αs-exprs␈αfrom␈αless␈αcomplicated␈αs-exprs.␈α ␈↓αcons␈↓␈αis␈α
a␈αtotal␈αfunction,␈αthat␈αis␈αit␈αis␈αdefined␈α
for␈αall
␈↓sexpr␈αarguments.␈αIt␈αis␈αa␈αfunction␈αof␈αtwo␈αarguments␈αand␈αhas␈αas␈αvalue␈αa␈αnew␈αsexpr␈αinterpreted␈αas␈αa
␈↓binary␈α
tree␈α
with␈α
left␈α
branch␈α
as␈α
the␈αfirst␈α
argument␈α
and␈α
with␈α
right␈α
branch,␈α
the␈α
second␈αargument.␈α
 For
␈↓example:
␈↓␈↓ ¬O␈↓αcons[A; B] = (A . B)

␈↓α␈↓ ¬⊃cons[(A . B); C] = ((A . B) .C)


␈↓We␈α∂have␈α∂two␈α∂unary␈α∂selector␈α∂functions,␈α∂␈↓αcar␈↓␈α⊂and␈α∂␈↓αcdr␈↓,␈α∂for␈α∂traversing␈α∂binary␈α∂trees.␈α∂ They␈α⊂are␈α∂both
␈↓partial␈α∞functions;␈α∞that␈α∞is,␈α∞they␈α∞are␈α∞functions␈α∞which␈α∞are␈α∞␈↓↓undefined␈↓␈α∞for␈α∞some␈α∞arguments.␈α∞ ␈↓π 1␈↓␈α∞These
␈↓LISP␈α∃functions␈α∃are␈α∃defined␈α∃only␈α∃for␈α∃non-atomic␈α∃arguments.␈α∃ ␈↓αcar␈↓␈α∃returns␈α∃as␈α∃value␈α∃the␈α∃first
␈↓subexpression␈α∞of␈α∞its␈α∞(composite)␈α∞argument;␈α∞␈↓αcdr␈↓␈α
(pronounced␈α∞could-er)␈α∞returns␈α∞as␈α∞value␈α∞the␈α
second
␈↓sub-expression␈α
of␈α
its␈α
argument.␈α
 For␈α
example:

␈↓␈↓ ∧B␈↓αcar[(A . B)] = A        car[A] ␈↓is undefined.
␈↓␈↓ ∧≥␈↓αcdr[(A . B)] = B       cdr[(A . (B . C))] = (B . C)
␈↓α␈↓ ¬&car[((A . B) . C)] = (A . B)


␈↓As␈α
with␈α
most␈α
mathematical␈α
theories,␈α
we␈α
will␈α
allow␈α
functional␈α
composition.

␈↓α␈↓ ∧Jcar[cdr[(A . (B . C))]] = car[(B . C)] =  B
␈↓α␈↓ ∧Icdr[cdr[(A . (C . B))]] = cdr[(C . B)] =  B
␈↓α␈↓ ∧Scar[cons[x;y]] = x     cdr[cons[x;y]] = y .

␈↓Notice␈α∞that␈α∞in␈α
the␈α∞last␈α∞two␈α
examples␈α∞we␈α∞have␈α
introduced␈α∞variables␈α∞(over␈α
S-exprs).␈α∞ In␈α∞the␈α
sequel
␈↓lower-case␈αletters␈α(or␈αlower-case␈αidentifiers)␈αwill␈αbe␈αused␈αfreely␈αas␈αvariables.␈α So␈αfor␈αexample␈α␈↓αY␈↓␈αis␈αan
␈↓atom;␈α
␈↓αy␈↓␈α
is␈αa␈α
variable.␈α
 Perhaps␈αthe␈α
most␈α
natural␈α
way␈αto␈α
evaluate␈α
expressions␈αinvolving␈α
compositions
␈↓is␈αto␈αevaluate␈αthe␈αinner-most␈αexpressions␈αfirst,␈αthen␈αwork␈αoutwards.␈αArguments␈αto␈αfunctions␈αmay␈αbe
␈↓evaluated␈α
in␈α
any␈α
order.␈α
Thus:

␈↓α␈↓ α
cons[car[(A . B)];cdr[(A . (1 . 2))]] = cons[A;cdr[(A . (1 . 2))]] = cons[A;(1 . 2)] = (A .(1 . 2))
␈↓α␈↓ βM␈↓or:␈↓α  = cons[car[(A . B)];(1 . 2)] = cons[A;(1 . 2)] = (A .(1 . 2))

␈↓This␈αseems␈αto␈αbe␈αquite␈αa␈αsimple␈αoperation␈αbut␈αwe␈αshall␈αsoon␈αsee␈αthat␈αevaluation␈αis␈αa␈αvery␈αcomplex
␈↓process.

␈↓The␈αcomposition␈αof␈α
many␈α␈↓αcar␈↓␈αand␈α␈↓αcdr␈↓␈α
functions␈αoccurs␈αso␈αfrequently␈α
that␈αan␈αabbreviation␈αhas␈α
been
␈↓developed.



________________
␈↓␈↓π 1␈↓␈α
How␈α
"undefined"␈α∞manifests␈α
itself␈α
on␈α∞a␈α
machine␈α
will␈α∞depend␈α
on␈α
the␈α∞implementation.␈α
Sometimes
␈↓error␈α
messages␈α
are␈α
given;␈α
sometimes␈α
it␈α
results␈α
in␈α
an␈α
excursion␈α
into␈α
the␈α
subconscious.
␈↓␈↓↓2.3␈↓ 	λPrimitive Functions     9␈↓


␈↓For example:
␈↓α␈↓ ¬Gcadr[x] <= car[cdr[x]]
␈↓α␈↓ ¬!caddr[x] <= car[cdr[cdr[x]]]
␈↓α␈↓ ¬Gcdar[x] <= cdr[car[x]]

␈↓These␈α∞compositions␈α∞are␈α∞also␈α∞called␈α∞␈↓αcar-cdr-␈↓↓chains␈↓,␈α∞and␈α∞are␈α∞useful␈α∞in␈α∞traversing␈α∞binary␈α∞trees.␈α
The
␈↓"<="-␈α∞notation␈α∞is␈α∞to␈α∞be␈α∞read␈α∞"is␈α∞defined␈α∞to␈α∞be␈α∞the␈α∞function␈α∞..."␈α∞This␈α∞notation␈α∞is␈α∞only␈α∞a␈α
temporary
␈↓convenience␈αand␈αnot␈αpart␈αof␈αLISP.␈α Very␈αsoon␈αwe␈αwill␈αstudy␈αwhat␈αis␈αinvolved␈αin␈αgiving␈αand␈αusing
␈↓definitions␈α∞in␈α∞LISP␈α∞(Section␈α∞4.6).␈α∞ For␈α∞the␈α∞moment␈α∞intuition␈α∞should␈α
suffice.

␈↓The␈α
syntax␈α
of␈α
the␈α
LISP␈α
expressions␈α
(or␈α
forms)␈α
allowed␈α
so␈α
far␈α
is␈α
given␈α
below.

␈↓<form>␈↓ βλ::= <constant> | <function>[<arg>; ... <arg>] | <function>[] | <variable>

␈↓<constant>␈↓ βλ::= <sexpr>               (see page 4.)

␈↓<function>␈↓ βλ::= <identifier>

␈↓<arg>␈↓ βλ::= <form>

␈↓<variable>␈↓ βλ::= <identifier>

␈↓<identifier>␈↓ βλ::= <letter> | <identifier><letter> | <identifier><digit>

␈↓<letter>␈↓ βλ::= ␈↓αa | b | ... | z


␈↓We␈α∩will␈α∩frequently␈α∩violate␈α∩these␈α∩syntax␈α∩equations,␈α∩allowing␈α∩function␈α∩names␈α∩containing␈α∩special
␈↓characters,␈α∪e.g.␈α∀␈↓αfact*␈↓,␈α∪␈↓αfib␈↓β1␈↓␈α∀or␈α∪+␈α∪;␈α∀or␈α∪writing␈α∀␈↓αx␈α∪+y␈↓␈α∪instead␈α∀of␈α∪␈↓α+[x;y]␈↓.␈α∀ Rather␈α∪than␈α∀attempt␈α∪to
␈↓characterize␈α
these␈α
violations,␈α
occurrences␈α
of␈α
such␈α
should␈α
be␈α
clear␈α
from␈α
context.

␈↓Notice␈α∂that␈α∂the␈α∂class␈α∂<form>␈α∂is␈α∂a␈α∞collection␈α∂of␈α∂LISP␈α∂expressions␈α∂which␈α∂can␈α∂be␈α∂evaluated;␈α∞either
␈↓constants,␈α∩function␈α∩name␈α∪followed␈α∩by␈α∩arguments,␈α∪or␈α∩a␈α∩variable␈α∪which␈α∩should␈α∩have␈α∪a␈α∩current
␈↓binding␈α
or␈α
value.␈α
Again␈α
we␈α
will␈α
wait␈α
to␈α
Section␈α
4.6␈α
for␈α
a␈α
precise␈α
description.

␈↓An␈α∩important␈α∪restraint␈α∩on␈α∩LISP␈α∪forms␈α∩which␈α∩is␈α∪not␈α∩covered␈α∩by␈α∪the␈α∩syntax␈α∩equations␈α∪is␈α∩the
␈↓requirement␈αthat␈αfunctions␈αare␈αdefined␈αas␈αbeing␈αn-ary␈αfor␈αsome␈α␈↓↓fixed␈↓␈αn.␈αn-ary␈αfunctions␈αmust␈αhave
␈↓␈↓↓exactly␈↓␈αn␈α
arguments␈αpresented␈α
to␈αthem␈α
whenever␈αthey␈α
are␈αapplied.␈α
 Thus␈α␈↓αcons[A],␈αcons[A;B;C],␈α
␈↓and
␈↓␈↓αcar[A;B]␈↓␈α
are␈α
all␈α
ill-formed␈α
expressions.
␈↓␈↓↓10  Symbolic expressions␈↓ 22.4␈↓


␈↓␈↓ ¬∂␈↓↓2.4  Conditional Expressions␈↓


␈↓We␈α∂cannot␈α∂generate␈α∂a␈α∂very␈α∂exciting␈α∂theory␈α⊂based␈α∂simply␈α∂on␈α∂␈↓αcar,␈α∂cdr,␈↓␈α∂and␈α∂␈↓αcons␈α∂␈↓␈α⊂with␈α∂functional
␈↓composition.␈α⊂Before␈α⊂we␈α⊂can␈α∂write␈α⊂reasonably␈α⊂interesting␈α⊂algorithms␈α∂we␈α⊂must␈α⊂have␈α⊂some␈α⊂way␈α∂of
␈↓performing␈α∞conditional␈α∞actions.␈α∞ To␈α∞do␈α∞this␈α∞we␈α∞need␈α∞predicates.␈α∞A␈α∞predicate␈α∞is␈α∞simply␈α∞a␈α
function
␈↓returning␈α∂a␈α∂value␈α∂representing␈α∂truth␈α∂or␈α∂falsity.␈α∂ Since␈α∂our␈α∂functions␈α∂return␈α∂Sexprs␈α∂as␈α∂values␈α∞we
␈↓must␈α
represent␈α
truth␈α
and␈α
falsity␈α
as␈α
Sexprs.␈α
 We␈α
will␈α
take␈α
the␈α
atoms␈α
␈↓αT␈↓␈α
and␈α
␈↓αNIL␈↓␈α
to␈α
represent␈αtrue
␈↓and␈α
false,␈α
respectively.␈α
 Notice␈αthat␈α
since␈α
predicates␈α
␈↓↓are␈↓␈α
functions,␈αthey␈α
may␈α
be␈α
freely␈αcomposed␈α
with
␈↓other␈α
functions.

␈↓LISP␈αhas␈αtwo␈αprimitive␈αpredicates.␈α The␈αfirst␈αis␈αa␈αtotal␈αpredicate␈αnamed␈α␈↓αatom␈↓.␈α It␈αis␈αa␈αpredicate␈αof
␈↓one␈α∞argument,␈α∞returning␈α∞␈↓αT␈↓␈α∞or␈α∂␈↓αNIL␈↓␈α∞depending␈α∞on␈α∞whether␈α∞or␈α∂not␈α∞the␈α∞value␈α∞of␈α∞that␈α∂argument␈α∞is
␈↓atomic.

␈↓α␈↓ ¬)atom[A] = atom[NIL] = T
␈↓α␈↓ ∧Katom[atom[A]] = T = atom[atom[(A . B)]]

␈↓The␈αsecond␈αprimitive␈αpredicate␈αis␈αnamed␈α␈↓αeq␈↓.␈α It␈αis␈αa␈αbinary␈αpredicate,␈αdefined␈αonly␈αif␈αits␈αarguments
␈↓are␈α
both␈α
atomic.␈α
 It␈α
returns␈α
␈↓αT␈↓␈α
if␈α
the␈α
arguments␈α
evaluate␈α
to␈α
the␈α
same␈α
atom;␈α
it␈α
returns␈α
␈↓αNIL␈↓␈α
otherwise.

␈↓α␈↓ ∧;eq [A;A] = T                   eq [A;B] = NIL
␈↓α␈↓ βjeq [(A . B); A] ␈↓ is undefined, as is    ␈↓αeq [(A . B);(A . B)]
␈↓α␈↓ ¬)eq [eq [A;B];eq [C;D]] = T

␈↓The␈α
IF-THEN-ELSE␈α
operation␈α
in␈α
LISP␈α
is␈α
called␈α
the␈α
conditional␈α
expression.␈α
 It␈α
is␈α
written:
␈↓α␈↓ ¬	[p␈↓β1␈↓α → e␈↓β1␈↓α; p␈↓β2␈↓α → e␈↓β2␈↓α  ... ; p␈↓βn␈↓α  → e␈↓βn␈↓α]

␈↓The␈αmeaning␈α(or␈αsemantics)␈αof␈αconditionals␈αis:␈αEach␈α␈↓αp␈↓βi␈↓␈αis␈αa␈αpredicate;␈αeach␈α␈↓αe␈↓βi␈↓␈αis␈αan␈αexpression.␈α We
␈↓evaluate␈αthe␈α␈↓αp␈↓βi␈↓␈α's␈αfrom␈αleft␈αto␈αright,␈αfinding␈αthe␈αfirst␈αwhich␈αreturns␈αvalue␈α␈↓αT␈↓.␈α When␈αwe␈αfind␈αsuch␈αa
␈↓␈↓αp␈↓βi␈↓␈α∩,␈α∩we␈α∩evaluate␈α∪the␈α∩corresponding␈α∩␈↓αe␈↓βi␈↓.␈α∩ The␈α∩value␈α∪of␈α∩the␈α∩conditional␈α∩expression␈α∩is␈α∪the␈α∩value
␈↓computed␈α∂by␈α∂␈↓αe␈↓βi␈↓;␈α∂if␈α∂none␈α⊂of␈α∂the␈α∂␈↓αp␈↓βi␈↓'s␈α∂are␈α∂true␈α⊂then␈α∂the␈α∂conditional␈α∂expression␈α∂is␈α⊂undefined.␈α∂ The
␈↓conditional␈α
expression␈α
is␈α
also␈α
undefined␈α
if␈α
we␈α
come␈α
across␈α
a␈α
␈↓αp␈↓βi␈↓␈α
which␈α
is␈α
undefined.
␈↓Examples:

␈↓α␈↓ ∧X[atom [A] → B; eq [A;(A . B)] → C] = B
␈↓α␈↓ ∧![eq [A;(A . B)] → C; atom [A] → B] is undefined
␈↓α␈↓ αo[atom [(A . B)] → B; eq [A ; B] → C; eq [car[(A . B)]; cdr[(B . A)]] → E] = E

␈↓Notice␈αthat␈αthe␈αp␈↓β2␈↓␈αexpression␈α
of␈αthe␈αfirst␈αexample␈αis␈α
undefined,␈αbut␈αthe␈αconditional␈αgives␈α
value␈α␈↓αB␈↓
␈↓since␈α
p␈↓β1␈↓␈α
gives␈α
value␈α
␈↓αT␈↓.

␈↓In␈αapplications␈α
of␈αconditional␈αexpressions␈α
it␈αis␈αoften␈α
convenient␈αto␈αbe␈α
assured␈αthat␈α
the␈αconditional
␈↓always␈αis␈αdefined.␈α To␈αmake␈αsure␈αthat␈αat␈αleast␈αone␈αof␈αthe␈α␈↓αp␈↓βi␈↓'s␈αis␈αtrue␈αwe␈αcan␈αpick␈αa␈αpredicate␈αfor␈α
␈↓αp␈↓βn␈↓,
␈↓(the␈α∞last␈α∂predicate␈α∞in␈α∞the␈α∂conditional)␈α∞which␈α∂is␈α∞always␈α∞true.␈α∂ You␈α∞can␈α∞think␈α∂of␈α∞lots␈α∂of␈α∞predicates
␈↓␈↓↓2.4␈↓ λ@Conditional Expressions     11␈↓


␈↓which␈α∞are␈α∞always␈α
true␈α∞(␈α∞for␈α
example,␈α∞␈↓αeq␈α∞[1;1]␈↓␈α∞).␈α
 A␈α∞natural␈α∞predicate␈α
is␈α∞the␈α∞constant␈α∞predicate,␈α
␈↓αT␈↓.
␈↓Thus:

␈↓α␈↓ ¬][p␈↓β1␈↓α  → e␈↓β1␈↓α; T → e␈↓β2␈↓α]

␈↓can␈α
be␈α
read:␈α
If␈α
␈↓αp␈↓β1␈↓␈α
is␈α
true␈α
then␈α
␈↓αe␈↓β1␈↓␈α
else␈α
␈↓αe␈↓β2␈↓.␈α
 (or␈α
otherwise␈α
␈↓αe␈↓β2␈↓)

␈↓Some␈α
care␈αhas␈α
to␈α
be␈αgiven␈α
to␈α
the␈αdefintion␈α
of␈α
the␈αmeaning␈α
of␈α
a␈αconditional␈α
expression.␈α
 First␈αthe
␈↓order␈α↔of␈α⊗evaluation␈α↔␈↓↓is␈↓␈α↔important.␈α⊗Consider:␈α↔␈↓αf[x;y] <= [y =0 → 0; T → x/y]␈↓.␈α⊗If␈α↔we␈α↔insisted␈α⊗on
␈↓evaluating␈αall␈αof␈αthe␈αconstitutients␈αof␈αthe␈αconditional␈αfirst,␈αthen␈α␈↓αf[␈α...;0]␈↓␈αwould␈αbe␈αundefined,␈αrather
␈↓than␈α
giving␈α
value␈α
␈↓α0␈↓␈α
as␈α
our␈α
current␈α
interpretation␈α
would.

␈↓There␈α#are␈α#other␈α#interpretations␈α$of␈α#equal␈α#merit.␈α#Consider␈α#the␈α$nonsense␈α#definition:
␈↓␈↓αg[x;y] <= [lic[x] → 1;T → 1]␈↓.␈αClearly,␈αassuming␈αthat␈α␈↓αlic␈↓␈αis␈αa␈αtotal␈αpredicate,␈αany␈αvalue␈αcomputed␈αby␈α␈↓αg␈↓
␈↓will␈α↔be␈α↔␈↓α1␈↓.␈α↔But␈α↔requiring␈α↔left-to-right␈α↔evaluation␈α↔could␈α↔spend␈α↔a␈α↔great␈α↔deal␈α↔of␈α⊗unnecessary
␈↓computation␈α
if␈α
␈↓αlic␈↓␈α
is␈α
a␈α
␈↓↓l␈↓ong␈α
␈↓↓i␈↓nvolved␈α
␈↓↓c␈↓alculation.

␈↓Questions␈α∂of␈α∂evaluation␈α∂are␈α∂non-trivial.␈α∂You␈α∂should␈α∞at␈α∂least␈α∂be␈α∂aware␈α∂that␈α∂some␈α∂decisions␈α∞have
␈↓been␈α
made␈α
and␈α
others␈α
were␈α
possible.

␈↓A␈αword␈αto␈αthe␈α
wise.␈α It␈αdoesn't␈αseem␈αlike␈α
you␈αcan␈αdo␈αmuch␈αdamage␈α
with␈αsuch␈αa␈αlimited␈αcollection␈α
of
␈↓operations.␈α
 ␈↓¬Do␈α
not␈αbe␈α
deceived!␈↓␈α
In␈αelementary␈α
number␈α
theory␈αall␈α
you␈α
have␈αis␈α
zero␈α
and␈αsome␈α
simple
␈↓functions,␈αand␈αelementary␈α
number␈αtheory␈αis␈αfar␈α
from␈αelementary.␈α Manipulation␈αof␈α
our␈αprimitives,
␈↓with␈α⊃composition,␈α⊃and␈α⊃conditional␈α⊃expressions,␈α⊂coupled␈α⊃with␈α⊃techniques␈α⊃for␈α⊃definition␈α⊃can␈α⊂␈↓↓also␈↓
␈↓become␈α
complicated.

␈↓For␈αexample:␈α
our␈αpredicate␈α
␈↓αeq␈↓␈αis␈αdefined␈α
only␈αfor␈α
atomic␈αarguments.␈α We␈α
would␈αlike␈α
to␈αbe␈α
able␈αto
␈↓test␈α⊂for␈α⊂equality␈α⊂of␈α∂arbitrary␈α⊂sexprs.␈α⊂ By␈α⊂equality␈α∂we␈α⊂mean:␈α⊂as␈α⊂trees,␈α∂the␈α⊂sexprs␈α⊂have␈α⊂the␈α∂same
␈↓branching␈α
structure;␈α
and␈αthe␈α
corresponding␈α
terminal␈αnodes␈α
are␈α
labeled␈αby␈α
the␈α
same␈α
atoms.␈α Thus,
␈↓we␈α
would␈α
like␈α
to␈α
define␈α
a␈α
predicate,␈α
␈↓αequal␈↓,␈α
such␈α
that:

␈↓α␈↓ ∧Jequal [(A . B);(A . B)] = T = equal [A;A]
␈↓α␈↓ ¬∪equal [(A . B);(B . A)] = NIL
␈↓α␈↓ ∧cequal [(A . (B . C));(A . (B . C))] = T


␈↓Here's␈α
an␈α
intuitive␈α
description␈α
of␈α
such␈α
a␈α
function␈α
(predicate)␈α
named␈α
␈↓αequal␈↓.

␈↓1.␈α If␈αboth␈αarguments␈α
are␈αatomic␈αthen␈αsee␈αwhat␈α
␈↓αeq␈↓␈αsays␈αabout␈αthem␈α(are␈α
they␈α"␈↓αeq␈↓").␈α We␈αcan␈α
test␈αif
␈↓␈↓ αλthey␈α
are␈α
both␈α
atomic␈α
by␈α
using␈α
␈↓αatom␈↓␈α
and␈α
a␈α
conditional␈α
expression.

␈↓2.␈α
 If␈α
one␈α
is␈α
atomic␈α
and␈α
the␈α
other␈α
is␈α
not␈α
they␈α
can't␈α
be␈α
equal␈α
s-exprs.

␈↓3.␈α⊂ Otherwise␈α⊂both␈α⊂are␈α⊂non-atomic␈α⊂sexprs.␈α∂ Both␈α⊂have␈α⊂two␈α⊂sub-expressions.␈α⊂ Look␈α⊂at␈α⊂both␈α∂first
␈↓␈↓ α_subexpressions.␈α_ If␈α_the␈α_first␈α_sub-expressions␈α↔are␈α_not␈α_equal␈α_then␈α_certainly␈α_the␈α↔initial
␈↓␈↓↓12  Symbolic expressions␈↓ 22.4␈↓


␈↓␈↓ α_expressions␈αcannot␈α
hope␈αto␈αbe␈α
equal.␈α If,␈αhowever,␈α
the␈αfirst␈αsubexpressions␈α
are␈αequal␈αthen␈α
the
␈↓␈↓ α_question␈α⊂of␈α⊂whether␈α⊂or␈α⊂not␈α⊂the␈α⊂initial␈α⊂expressions␈α⊂are␈α⊂equal␈α⊂depends␈α⊂on␈α⊂the␈α⊂equality␈α∂(or
␈↓␈↓ α_non-equality)␈α
of␈α
the␈α
second␈α
subexpressions.␈α
 Thus␈α
the␈α
following␈α
definition:

␈↓α     equal[x;y] <=␈↓ βh[atom[x] → [atom[y] → eq [x;y];
␈↓α␈↓ βh␈↓ ¬_ T→ NIL];
␈↓α␈↓ βh atom[y] → NIL;
␈↓α␈↓ βh equal [car[x];car[y]] → equal[cdr[x];cdr[y]];
␈↓α␈↓ βh T → NIL]


␈↓α␈↓Notice␈αthat␈αwe␈α
use␈αnested␈αconditional␈α
expressions␈αin␈α␈↓αequal␈↓;␈α
e␈↓β1␈↓␈αis␈αitself␈α
a␈αconditional.␈αAlso␈α
we␈αhave
␈↓used␈α⊃predicates␈α⊃in␈α∩the␈α⊃e␈↓βi␈↓␈α⊃positions␈α∩at␈α⊃e␈↓β3␈↓␈α⊃and␈α⊃e␈↓β11␈↓;␈α∩this␈α⊃is␈α⊃perfectly␈α∩reasonable␈α⊃since␈α⊃␈↓αequal␈↓␈α∩is␈α⊃a
␈↓predicate␈α
and␈α
thus␈α
returns␈α
either␈α
␈↓αNIL␈↓␈α
or␈α
␈↓αT␈↓.

␈↓Let's␈α∪show␈α∪that␈α∪␈↓αequal␈↓␈α∪does␈α∩perform␈α∪correctly␈α∪for␈α∪a␈α∪specific␈α∩example.␈α∪ This␈α∪will␈α∪also␈α∪show␈α∩a
␈↓complicated␈α
evaluation␈α
of␈α
a␈α
conditional␈α
expression.

␈↓αequal[(A . B);(A . C)] =␈↓ ∧H[atom[(A . B)] → [atom[(A . C)] → eq [(A . B);(A . C)];
␈↓α␈↓ ∧H␈↓ εx T→ NIL];
␈↓α␈↓ ∧H atom[(A . C)] → NIL;
␈↓α␈↓ ∧H equal [car[(A . B)];car[(A . C)]] → equal[cdr[(A . B)];cdr[(A . C)]];
␈↓α␈↓ ∧H T → NIL]

␈↓Now␈α⊂using␈α⊃the␈α⊂meaning␈α⊃of␈α⊂conditionals␈α⊃(page␈α⊂10),␈α⊂we␈α⊃find␈α⊂that␈α⊃p␈↓β1␈↓␈α⊂(i.e., ␈↓αatom[(A . B)]␈↓ )␈α⊃and␈α⊂p␈↓β2␈↓
␈↓( ␈↓αatom[(A . C)]␈↓ )␈α when␈α!evaluated␈α (in␈α order)␈α!give␈α ␈↓αNIL␈↓.␈α  We␈α!must␈α now␈α!evaluate␈α p␈↓β3␈↓:
␈↓␈↓αequal[car[(A . B)];car[(A . C)]]␈↓.␈α
 This␈α
reduces␈α
to␈α
␈↓αequal[A;A]␈↓,␈α
and:

␈↓α   equal[A;A] =␈↓ ∧H[atom[A] → [atom[A] → eq[A;A];
␈↓α␈↓ ∧H␈↓ εx T→ NIL];
␈↓α␈↓ ∧H atom[A] → NIL;
␈↓α␈↓ ∧H equal [car[A];car[A]] → equal[cdr[A];cdr[A]];
␈↓α␈↓ ∧H T → NIL]

␈↓This␈α≠conditional␈α≠expression␈α~will␈α≠finally␈α≠evaluate␈α≠to␈α~␈↓αT␈↓.␈α≠So␈α≠p␈↓β3␈↓␈α~in␈α≠the␈α≠original␈α≠call␈α~of
␈↓␈↓αequal[(A . B);(A . C)]␈↓␈α≡is␈α≡true,␈α≡and␈α∨we␈α≡are␈α≡face␈α≡with␈α∨the␈α≡evaluation␈α≡of␈α≡e␈↓β3␈↓␈α∨which␈α≡is
␈↓␈↓αequal[cdr[(A . B);cdr(A . C)]]␈↓.␈α
After␈α
evaluation␈α
of␈α
the␈αarguments␈α
and␈α
evaluation␈α
of␈α
the␈αconditional
␈↓expression␈α∞defining␈α
␈↓αequal␈↓␈α∞we␈α
will␈α∞finally␈α
return␈α∞value␈α
␈↓αNIL␈↓.␈α∞That␈α
is␈α∞␈↓α(A␈α
.␈α∞B)␈↓␈α
and␈α∞␈↓α(A␈α
.␈α∞C)␈↓␈α∞are␈α
␈↓↓not␈↓
␈↓equal.␈α
 Clearly␈α
evaluation␈α
of␈α
LISP␈αexpressions␈α
in␈α
this␈α
great␈α
detail␈α
is␈αnot␈α
a␈α
process␈α
which␈α
we␈αwish␈α
to
␈↓do␈α
very␈α
often.

␈↓Notice␈α∪that␈α∪throughout␈α∪this␈α∪example␈α∪expressions␈α∪like␈α∪␈↓αatom[(A . B)]␈↓␈α∪or␈α∪␈↓αeq[(A . B);(A . C)]␈↓␈α∪were
␈↓appearing␈α∩but␈α∪were␈α∩never␈α∪evaluated␈α∩because␈α∪of␈α∩the␈α∩way␈α∪in␈α∩which␈α∪we␈α∩defined␈α∪evaluation␈α∩of
␈↓conditionals.

␈↓Finally,␈α
to␈α
include␈α
conditional␈α
expressions␈α
in␈α
our␈α
syntax␈α
of␈α
LISP␈α
expressions,␈α
we␈α
should␈α
add:
␈↓␈↓↓2.4␈↓ λ@Conditional Expressions     13␈↓


␈↓<form>␈↓ αh::= [<form> → <form>; ... <form> → <form>]         (see page 9)




␈↓␈↓ ¬u␈↓↓2.5  Problems␈↓

␈↓I Evaluate the following

␈↓␈↓ βR␈↓↓1.␈↓α eq[X;Y]   ␈↓↓2.␈↓α cons[X;Y]   ␈↓↓3.␈↓α car[(X . Y)]   ␈↓↓4.␈↓α car[cons[X;Y]]

␈↓α␈↓↓5.␈↓α cadr[(X .(Y . NIL))]␈↓ ε8␈↓↓6.␈↓α cdar[(X .(Y . NIL))]

␈↓α␈↓↓7.␈↓α eq[cdr[(A . B)];cdr[(C . B)]]␈↓ ε8␈↓↓8.␈↓α atom[cons[(A . B);(C . D)]]

␈↓α␈↓↓9.␈↓α cons[atom[A];atom[(A . B)]]␈↓ ε8␈↓↓10.␈↓α eq[atom[ATOM];atom[EQ]]

␈↓α␈↓ βD␈↓↓11.␈↓α [T → A; T → B]   ␈↓↓12.␈↓α [NIL → A; T → B]   ␈↓↓13.␈↓α [eq[A;B] → 4]

␈↓α␈↓↓14.␈↓α [atom[X] → atom[X]; T → FOO]␈↓ ε8␈↓↓15.␈↓α [eq[EQ;X] → A; eq[A;B] → B; T → C]

␈↓α␈↓ β↑␈↓↓16.␈↓α cons[[eq[A;B] → 1; T → FOO];cons[A;cadr[(A .(B .C))]]]

␈↓α␈↓↓17.␈↓α equal[(A . B);(A . B)]␈↓ ε8␈↓↓18.␈↓α eq[(A . B);(A . B)]

␈↓II  Use the following definition:

␈↓α␈↓ αX  twist[s] <=␈↓ ∧([atom[s] → s;
␈↓α␈↓ αX␈↓ ∧( T → cons[twist[cdr[s]];twist[car[s]]]]

␈↓α␈↓↓1␈↓. Is the function partial or is it total?

␈↓Now evaluate:
␈↓␈↓↓2.␈↓α twist[A]   ␈↓↓3.␈↓α twist[(A . B)]   ␈↓↓4.␈↓α twist[((A . B). C)]

␈↓III  Now try:
␈↓α␈↓ αXfindem[x;y] <=␈↓ ∧([atom[x] → [eq[x;y] → T; T → NIL];
␈↓α␈↓ αX␈↓ ∧( T → cons[findem[car[x];y];findem[cdr[x];y]]]


␈↓α␈↓↓1␈↓.  Is this function total?

␈↓and now evaluate:
␈↓␈↓ α⎇␈↓↓2.␈↓α findem[(A . B);A]   ␈↓↓3.␈↓α findem[(B .(A . C));A]  ␈↓↓4.␈↓α findem[(B .(A . C));C]
␈↓α␈↓ ¬-␈↓↓5.␈↓α findem[(A . B);(A . B)]
␈↓␈↓↓14  Symbolic expressions␈↓ 52.6␈↓


␈↓␈↓ ¬Z␈↓↓2.6  List Notation␈↓


␈↓In␈α
many␈α
applications␈α
of␈α
LISP␈α
it␈α
will␈α
be␈α
very␈α
convenient␈α
to␈α
represent␈α
sequences.␈α
 E.g.␈α
␈↓α(x␈↓β1␈↓α,␈α∞x␈↓β2␈↓α,␈α
x␈↓β3␈↓α)␈↓.
␈↓We␈α∞will␈α∂allow␈α∞sequences␈α∞to␈α∂have␈α∞sub-sequences␈α∞(to␈α∂an␈α∞arbitrary␈α∞depth)␈α∂e.g.␈α∞␈↓α(A, (B, C), D, (E, B))␈↓.
␈↓The␈αobvious␈α
question␈αis␈α
how␈αto␈α
represent␈αsequences␈α
as␈αSexprs.␈α
 Since␈αsequences␈α
can␈αbe␈αof␈α
arbitrary
␈↓length␈α∂any␈α∂representation␈α∂must␈α∂include␈α∂an␈α∂unambiguous␈α∂way␈α∂of␈α∂determining␈α∂the␈α∂end␈α∂of␈α⊂such␈α∂a
␈↓sequence.

␈↓The␈α
choice␈α
we␈α
make␈α
is␈α
represented␈α
graphically␈α
as␈α
follows:␈α
␈↓α


␈↓α(X, Y, Z)  ≡                                                      ≡ (X  . (Y  . (Z  . NIL)))  

␈↓α                  X               Y               Z      NIL


␈↓α␈↓Or:






␈↓α                 X           Y          Z          NIL

␈↓That␈αis␈αthe␈αright-hand␈αbranch␈αin␈αany␈αbinary␈αtree␈αrepresenting␈αa␈αsequence␈αwill␈αalways␈αpoint␈αto␈αthe
␈↓rest␈αof␈αthe␈α
sequence␈αor␈αwill␈αbe␈α
the␈αatom␈α␈↓αNIL␈↓.␈α
 It␈αis␈αnot␈αby␈α
accident␈αthat␈αthe␈α
atom␈α␈↓αNIL␈↓␈αis␈αused␈α
both
␈↓for␈α⊂falsity␈α⊂and␈α∂the␈α⊂termination␈α⊂symbol␈α∂for␈α⊂sequences.␈α⊂ You␈α∂should␈α⊂become␈α⊂fluent␈α⊂in␈α∂translating
␈↓between␈α
sexpr-notation␈α
and␈α
sequence␈α
notation.

␈↓In␈α
more␈α
standard␈α
terminology␈α
sequences␈α
are␈α
called␈α
lists,␈α
thus␈α
we␈α
will␈α
refer␈α
usually␈α
to␈αlist-notation,
␈↓list␈αterminator,␈αetc.␈α You␈αshould␈αalso␈αbecome␈αfluent␈αin␈αapplying␈αour␈αbasic␈αfunctions␈αto␈αlists␈αwithout
␈↓having␈α
to␈α
translate␈α
back␈α
to␈α
sexpr-notation.

␈↓Notice␈αthat␈α
the␈α␈↓αcar-cdr␈↓-chains␈αare␈α
very␈αuseful␈α
for␈αobtaining␈αelements␈α
of␈αa␈αlist.␈α
 To␈αretrieve␈α
the␈αn␈↓πth␈↓
␈↓element␈α
of␈α
a␈α
list␈α
use␈α
␈↓αcad␈↓πn-1␈↓αr.␈α
 ␈↓

␈↓A␈α
final␈αpoint:␈α
what␈α
about␈αthe␈α
empty␈α
sequence␈αor␈α
empty␈α
list?␈αLooking␈α
at␈α
the␈αgraphical␈α
interpretation
␈↓of␈αa␈α
sequence␈αit␈αappears␈α
natural␈αto␈αtake␈α
␈↓αNIL␈↓␈αas␈α
the␈αempty␈αlist␈α
since␈αafter␈αyou␈α
have␈αremoved␈αall␈α
the
␈↓elements␈α⊂from␈α⊂the␈α∂list,␈α⊂␈↓αNIL␈↓,␈α⊂the␈α∂list␈α⊂terminator␈α⊂is␈α⊂all␈α∂that␈α⊂is␈α⊂left.␈α∂ Also␈α⊂from␈α⊂the␈α⊂standpoint␈α∂of
␈↓sequence␈α
notation,␈α
the␈α
empty␈α
sequence␈α
is␈α
represented␈αas:␈α
"␈↓α(␈α
)␈↓".␈α
 To␈α
be␈α
consistent,␈α
then,␈α
we␈αwill␈α
define
␈↓␈↓α(␈α∂)␈↓␈α∂to␈α∂be␈α∂the␈α∂same␈α∂as␈α∂the␈α∂atom␈α∂␈↓αNIL␈↓.␈α∂ And␈α∂a␈α∂notational␈α∂point:␈α∂in␈α∂graphical␈α∂notation␈α∂it␈α∂is␈α∞often
␈↓convenient␈α
to␈α
write
␈↓␈↓↓2.6␈↓ 	TList Notation     15␈↓



␈↓␈↓ αX␈↓ ¬_as                  .

␈↓␈↓ αX       ␈↓αNIL␈↓
␈↓Thus, for example:

␈↓␈↓ ¬g␈↓α(A, (B, C), D)␈↓ is:



␈↓α␈↓ βxA␈↓ ¬_␈↓ ε8    D



␈↓α␈↓ βx␈↓ ¬_   B␈↓ ε8   C

␈↓or:

␈↓α␈↓ εFA








␈↓α␈↓ ¬βB    C    NIL     D          NIL

␈↓Finally,␈α
in␈α
list-notation␈α
the␈α
commas␈α
can␈α
be␈α
replaced␈α
by␈α
spaces.
␈↓α␈↓ ¬εe.g. (A, (B, C), D) ≡ (A(B C)D).

␈↓Note:␈α
the␈α
"dots"␈α
in␈α
dot-notation␈α
are␈α
␈↓↓never␈↓␈α
optional.



␈↓␈↓ ∧Z␈↓↓2.7  Problems involving list-notation␈↓

␈↓I  Translate the following lists into Sexpr dotted-pair notation.
␈↓␈↓ βo␈↓↓1.␈↓α (A B C)   ␈↓↓2.␈↓α (A)   ␈↓↓3.␈↓α ((A))   ␈↓↓4.␈↓α (A (B (C)))  ␈↓↓5.␈↓α (NIL).

␈↓Now go the other way and translate the following sexprs into list notation.
␈↓␈↓ βt␈↓↓5.␈↓α ((A .(B . NIL)).((C . NIL). NIL))   ␈↓↓6.␈↓α (NIL . NIL)
␈↓α␈↓ ∧M␈↓↓7.␈↓α (CONS .((QUOTE .(A . NIL)). NIL))
␈↓␈↓↓16  Symbolic expressions␈↓ 52.7␈↓



␈↓II  Evaluate the following, writing the results in list notation where possible.
␈↓␈↓ β<␈↓↓1.␈↓α car[(A B)]   ␈↓↓2.␈↓α cdr[(A B)]   ␈↓↓3.␈↓α cons[A;(B C)]   ␈↓↓4.␈↓α cons[A;NIL]
␈↓α␈↓ ¬6␈↓↓5.␈↓α cons[eq[A;A];(A B C)]

␈↓III␈α∞Construct␈α∞an␈α∞expression␈α∂evaluating␈α∞to␈α∞the␈α∞list,␈α∞␈↓α(A␈α∂B)␈↓,␈α∞using␈α∞only␈α∞the␈α∞primitive␈α∂functions␈α∞and
␈↓predicates,␈α
and␈α
the␈α
atoms␈α
␈↓αA␈↓␈α
and␈α
␈↓αB␈↓.



␈↓␈↓ ¬c␈↓↓2.8  ␈↓αlist␈↓↓ and ␈↓αnull␈↓↓␈↓α


␈↓We␈α
deal␈α
with␈αfunctions␈α
restricted␈α
to␈αlists␈α
frequently␈α
enough␈αthat␈α
it␈α
is␈αreasonable␈α
to␈α
define␈α
the␈αlist
␈↓counterpart␈α∞to␈α∞␈↓αatom␈↓,␈α∂that␈α∞is␈α∞a␈α∂predicate␈α∞which␈α∞asks,␈α∂"is␈α∞the␈α∞list␈α∂empty?".␈α∞ The␈α∞predicate␈α∂used␈α∞for
␈↓testing␈α
for␈α
the␈α
empty␈α
list␈α
is␈α
called␈α
␈↓αnull␈↓␈α
and␈α
can␈α
be␈α
defined␈α
as:

␈↓␈↓ ∧5␈↓αnull[x] <= [atom[x] → eq[x;NIL]; T → NIL].␈↓ 

␈↓Similarly,␈α∩it␈α⊃is␈α∩convenient␈α∩to␈α⊃have␈α∩a␈α⊃notation␈α∩representing␈α∩the␈α⊃construction␈α∩of␈α⊃a␈α∩list␈α∩from␈α⊃an
␈↓arbitrary␈α
number␈α
of␈α
sexpressions␈α
␈↓π 2␈↓.

␈↓␈↓αlist␈↓␈↓α[␈↓x␈↓β1␈↓;␈α
x␈↓β2␈↓;␈α
...␈α
x␈↓βn␈↓]␈α
generates␈α
a␈α
list␈α
consisting␈α
of␈α
the␈α
values␈α
of␈α
the␈α
n␈α
arguments.␈α
That␈α
is,␈α
the␈α
effect␈α
is:

␈↓␈↓ ∧d␈↓αcons[␈↓x␈↓β1␈↓α;cons[␈↓x␈↓β2␈↓α; ... cons[␈↓x␈↓βn␈↓α;NIL]] ...] ␈↓. 
␈↓Examples:
␈↓α␈↓ ¬blist[A;B] = (A B)
␈↓α␈↓ ¬Flist[A;B;C] = (A B C)
␈↓α␈↓ ∧Olist[cons[A;B];car[(A . B)]] = ((A . B) A)
␈↓α␈↓ ∧:list[A;list[B;C]] = list[A;(B C)] = (A (B C))
␈↓α␈↓ ε↓list[] = NIL
␈↓α␈↓ ¬Ylist[NIL] = (NIL)

␈↓Notice␈α⊂that␈α⊂␈↓αlist␈↓␈α⊃is␈α⊂␈↓↓not␈↓␈α⊂a␈α⊃function;␈α⊂it␈α⊂␈↓↓does␈↓␈α⊂evaluate␈α⊃its␈α⊂arguments,␈α⊂but␈α⊃it␈α⊂can␈α⊂take␈α⊃an␈α⊂arbitrary
␈↓number␈α
of␈α
them.␈α
 For␈α
the␈α
moment,␈α
␈↓αlist␈↓␈α
is␈α
simply␈α
a␈α
notational␈α
abbreviation␈α
for␈α
the␈α
nested␈α
␈↓αcons␈↓.



␈↓␈↓ ¬x␈↓↓2.9  A respite␈↓


␈↓This␈α∂section␈α∞contains␈α∂some␈α∂hints␈α∞and␈α∂notes␈α∞on␈α∂the␈α∂introductory␈α∞material␈α∂of␈α∞this␈α∂chapter.␈α∂First␈α∞a

________________
␈↓␈↓π 2␈↓␈α
Some␈αpeople␈α
restrict␈αlists␈α
so␈αthat␈α
elements␈αof␈α
a␈αlist␈α
are␈αeither␈α
atomic␈αor␈α
are␈αlists␈α
themselves.␈αWe
␈↓shall␈α
not␈α
do␈α
so.␈α
Elements␈α
of␈α
a␈α
list␈α
may␈α
be␈α
arbitrary␈α
sexprs.
␈↓␈↓↓2.9␈↓ 
∂A respite     17␈↓


␈↓reiteration␈αof␈α
the␈αprevious␈α
admonition:␈αthough␈αthis␈α
material␈αseems␈α
quite␈αstraightforward,␈α
the␈αnext
␈↓chapter␈α
will␈α
begin␈α
to␈α
show␈α
you␈α
that␈α
things␈α∞are␈α
not␈α
all␈α
that␈α
trivial.␈α
 LISP␈α
is␈α
quite␈α∞powerful.␈α
 The
␈↓preceeding␈α
material␈α
␈↓↓is␈↓␈α
basic␈α
and␈α
the␈α
sooner␈α
it␈α
becomes␈α
second␈α
nature␈α
to␈α
you␈α
the␈α
better.

␈↓Perhaps␈α
many␈α
of␈α
these␈α
hints␈α
you␈α
have␈α
discovered␈α
yourself.

␈↓In␈α⊂graphing␈α∂`list␈α⊂notation'␈α⊂first␈α∂discover␈α⊂how␈α⊂many␈α∂elements␈α⊂are␈α∂in␈α⊂the␈α⊂list.␈α∂ This␈α⊂can␈α⊂be␈α∂done
␈↓simply␈α⊂by␈α⊂counting␈α⊃parentheses.␈α⊂Then␈α⊂draw␈α⊃the␈α⊂spine␈α⊂of␈α⊃a␈α⊂tree␈α⊂which␈α⊃will␈α⊂have␈α⊂as␈α⊃many␈α⊂left
␈↓branches␈α⊂as␈α⊃there␈α⊂are␈α⊂elements.␈α⊃ The␈α⊂right␈α⊂branch␈α⊃␈↓↓always␈↓␈α⊂ends␈α⊂with␈α⊃␈↓αNIL␈↓.␈α⊂Then␈α⊃examine␈α⊂the
␈↓structure␈αof␈αeach␈αnode.␈α Notice␈αagain␈αthat␈αto␈αfind␈αthe␈αn␈↓πth␈↓␈αelement␈αin␈αa␈αlist,␈αuse␈α␈↓αcad␈↓πn-1␈↓αr␈↓.␈α Thus␈α␈↓αcdr␈↓␈αis
␈↓␈↓↓never␈↓␈α
the␈α
second␈α
element␈α
of␈α
a␈α
list.␈α
␈↓αcadr␈↓␈α
is␈α
the␈α
second␈α
element,␈α
␈↓αcdr␈↓␈α
is␈α
the␈α
␈↓↓rest␈↓␈α
of␈α
the␈α
list.

␈↓When␈α
evaluating␈α
or␈α
writing␈αfunctions␈α
or␈α
(predicates)␈α
␈↓↓always␈↓␈α
keep␈αin␈α
mind␈α
any␈α
restrictions␈α
of␈αthe
␈↓function:␈α∞is␈α∞it␈α∞partial,␈α∞is␈α∂it␈α∞total,␈α∞defined␈α∞only␈α∞for␈α∂lists,␈α∞are␈α∞there␈α∞restrictions␈α∞on␈α∂arguments;␈α∞when
␈↓taking␈α
␈↓αcar␈↓␈α
or␈α
␈↓αcdr␈↓␈α
is␈α
the␈α
argument␈α
non-atomic.

␈↓A␈αfew␈αtricks␈αwere␈αembedded␈αin␈αthe␈αproblem␈α
sets.␈α In␈αSection␈α2.5,␈αrecall␈αproblem␈α␈↓↓8␈↓.␈αThe␈α
composition
␈↓␈↓αatom[cons[␈α...]]␈↓␈αwill␈αalways␈αevaluate␈αto␈α␈↓αNIL␈↓␈αsince␈αthe␈αresult␈αof␈α␈↓αcons␈↓-ing␈αis␈αalways␈αnon-atomic.␈α In␈α␈↓↓10.␈↓,
␈↓we␈αused␈αidentifiers␈α
with␈αthe␈αsame␈α
letter␈αstrings␈αas␈α
predicate␈αnames,␈α␈↓αATOM␈↓␈α
and␈α␈↓αEQ␈↓.␈α␈↓αATOM␈↓␈αand␈α
␈↓αEQ␈↓
␈↓are␈α⊃perfectly␈α⊂good␈α⊃identifiers,␈α⊃and␈α⊂are␈α⊃␈↓↓not␈↓␈α⊃the␈α⊂LISP␈α⊃predicates.␈α⊃ ␈↓↓14.␈↓␈α⊂shows␈α⊃that␈α⊃predicates␈α⊂are
␈↓perfectly␈α∞good␈α∞expressions␈α∞to␈α∞evaluate;␈α∞e␈↓β1␈↓␈α∞is␈α∞a␈α∞predicate.␈α∞Similarly,␈α∞in␈α∞␈↓↓16.␈↓,␈α∞conditional␈α∞expressions
␈↓may␈α
appear␈α
within␈α
functional␈α
composition.

␈↓Notice␈αthat␈α␈↓αtwist␈↓␈αin␈αII␈αis␈αtotal␈αwhereas␈α␈↓αfindem␈↓␈αis␈αpartial.␈α ␈↓αfindem␈↓␈αis␈αpartial␈αsince␈α␈↓αy␈↓␈αmust␈α
be␈αatomic.
␈↓Both␈αfunctions␈αbuild␈αnew␈αtrees,␈α␈↓αtwistem␈↓␈αreverses␈αleft-␈αand␈αright-branches␈αrecursively;␈α␈↓αfindem␈↓␈αbuilds
␈↓a␈α
tree␈αwith␈α
the␈α
same␈αbranching␈α
structure␈α
as␈α␈↓αx␈↓,␈α
but␈α
the␈αterminal␈α
nodes␈α
contain␈α␈↓αT␈↓␈α
at␈α
the␈αpoints␈α
where
␈↓the␈α
atom␈α
␈↓αy␈↓␈α
appears␈α
in␈α
the␈α
original␈α
tree,␈α
and␈α
␈↓αNIL␈↓␈α
otherwise.␈α
 Perhaps␈α
you␈α
also␈α
noticed␈α∞in␈α
␈↓αfindem␈↓
␈↓that␈α3the␈α4construction:␈α3␈↓αatom[x] → eq[x;y]␈↓␈α3could␈α4have␈α3been␈α3used␈α4instead␈α3of
␈↓␈↓αatom[x] → [eq[x;y] → T; T → NIL]␈↓.␈α
Prove␈α
it.

␈↓In␈α
Section␈α
2.7␈α
you␈α
should␈α
have␈α
discovered␈α
that␈α
the␈α
value␈α
of:

␈↓␈↓ ¬␈↓αcons[␈↓x␈↓β1␈↓α,(␈↓x␈↓β2␈↓, ...x␈↓βn␈↓α)]␈↓ is: ␈↓α(␈↓x␈↓β1␈↓, x␈↓β2␈↓, x␈↓βn␈↓α). 

␈↓α␈↓Notice␈αthat␈α␈↓αlist[␈↓x␈↓β1␈↓α;(␈↓x␈↓β2␈↓,␈α...␈αx␈↓βn␈↓α)]␈↓␈αis␈α␈↓α(␈↓x␈↓β1␈↓α␈α(␈↓x␈↓β2␈↓␈α...␈αx␈↓βn␈↓α))␈↓.␈α So␈α␈↓αcons␈↓␈αwill␈αadd␈αa␈αnew␈αelement␈αto␈αthe␈αfront␈αof␈αan
␈↓existing␈α
list.␈α
␈↓αlist␈↓␈α
will␈α
create␈α
a␈α
new␈α
list␈α
whose␈α
elements␈α
will␈α
be␈α
the␈α
values␈α
of␈α
the␈α
arguments␈α
to␈α
␈↓αlist␈↓.

␈↓Be␈α∂clear␈α∂on␈α⊂the␈α∂difference␈α∂between␈α∂the␈α⊂empty␈α∂list,␈α∂␈↓αNIL␈↓,␈α∂and␈α⊂the␈α∂list␈α∂consisting␈α∂of␈α⊂␈↓αNIL␈↓,␈α∂␈↓α(NIL)␈↓.
␈↓␈↓α(NIL)␈↓␈αis␈αan␈αabbreviation␈αfor␈α␈↓α(NIL␈α.␈αNIL)␈↓,␈αwhich␈αcertainly␈αis␈αnot␈α␈↓αNIL␈↓.␈α List␈αnotation␈αis␈αalways␈αan
␈↓abbreviation␈α
and␈α
can␈α
always␈α
be␈α
translated␈α
back␈α
into␈α
a␈α
sexpr.
␈↓␈↓↓18  Applications and Examples␈↓ C3.␈↓


␈↓␈↓ ¬␈␈↓↓SECTION 3

␈↓↓␈↓ β.APPLICATIONS AND EXAMPLES OF LISP DEFINITIONS␈↓






␈↓␈↓ ¬`␈↓↓3.1  Introduction␈↓


␈↓We␈α∀have␈α∀already␈α∪seen␈α∀a␈α∀few␈α∪examples␈α∀of␈α∀definition␈α∪and␈α∀evaluation␈α∀of␈α∀non-primitive␈α∪LISP
␈↓functions.␈α This␈α
section␈αwill␈αspend␈α
a␈αgood␈α
deal␈αof␈αtime␈α
showing␈αdifferent␈α
styles␈αof␈αdefinition,␈α
giving
␈↓hints␈αabout␈αhow␈αto␈αwrite␈αLISP␈αfunctions,␈αand␈αincrease␈αyour␈αfamilarlity␈αwith␈αLISP.␈αFinally,␈αseveral
␈↓complete␈α
and␈α
non-trivial␈α
examples␈α
will␈α
be␈α
described␈α
as␈α
LISP␈α
functions.

␈↓The␈αstyle␈αof␈αdefinition␈αavailable␈αin␈αour␈αcurrent␈αsubset␈αof␈αLISP␈αis␈α␈↓↓definition␈αby␈αrecursion␈↓.␈αThat␈α
is,
␈↓a␈αtypical␈αLISP␈αdefinition␈αwill␈αcontain␈αapplications␈αof␈αthat␈αfunction␈αname␈αbeing␈αdefined.␈α Consider
␈↓the␈α
factorial␈α
function,␈α
n!.␈α
We␈α
usually␈α
characterize␈α
this␈α
function␈α
by␈α
saying:

␈↓␈↓↓1.␈↓␈α
The␈α
function␈α
is␈α
defined␈α
for␈α
non-negative␈α
integers.

␈↓␈↓↓2.␈↓␈α
The␈α
value␈α
of␈α
the␈α
function␈α
for␈α
0␈α
is␈α
1.

␈↓␈↓↓3␈↓.␈α
 Otherwise␈α
the␈α
value␈α
of␈α
n!␈α
is␈α
n␈α
times␈α
the␈α
value␈α
of␈α
n-1!.

␈↓The␈α
implication␈α
here␈α
is␈α
that␈α
it␈α
is␈α
somehow␈α
easier␈α
to␈α
compute␈α
n-1!␈α
than␈α
to␈α
compute␈α
n!.

␈↓Recall␈α
also␈αour␈α
example␈α
of␈α␈↓αequal␈↓␈α
on␈α
page␈α12.␈α
The␈αquestion␈α
of␈α
equality␈αfor␈α
two␈α
non-atomic␈αsexprs
␈↓was␈α
thrown␈α
back␈α
to␈α
the␈α
question␈α
of␈α
equality␈α
for␈α
their␈α
␈↓αcar␈↓s␈α
and␈α
␈↓αcdr␈↓s.

␈↓These␈α∩examples␈α∩are␈α∩typical␈α∩of␈α∩LISP␈α∩definitions.␈α∩ The␈α∩body␈α∩of␈α∩the␈α∩definiton␈α∩is␈α∩a␈α⊃conditional
␈↓expression␈αfirst␈αinvolving␈αa␈αfew␈αspecial␈αcases,␈αcalled␈α␈↓↓termination␈αconditions␈↓.␈α Then,␈αthe␈αremainder
␈↓of␈αthe␈α
conditional␈αcovers␈α
the␈αgeneral␈αcase--␈α
what␈αto␈α
do␈αif␈α
the␈αargument␈αto␈α
the␈αfunction␈α
is␈αnot␈αone␈α
of
␈↓the␈αspecial␈α
cases.␈α The␈αtermination␈α
case␈αfor␈α
the␈αfactorial␈αshould␈α
be:␈α"is␈α
the␈αargument␈α0";␈α
termination
␈↓for␈α
␈↓αequal␈↓␈α
involves:␈α
"are␈α
either␈α
of␈α
the␈α
arguments␈α
atomic?".

␈↓It␈α
should␈α
be␈α
clear␈α
how␈α
to␈α
write␈α
a␈α
definition␈α
of␈α
the␈α
factorial␈α
function:
␈↓α␈↓ ∧Afact[n] <= [eq[n;0] → 1; T → *[n;fact[n-1]]]


␈↓Notice␈α∩that␈α∪␈↓αfact␈↓␈α∩is␈α∪a␈α∩partial␈α∩function,␈α∪defined␈α∩only␈α∪for␈α∩non-negative␈α∩integer.␈α∪␈↓αequal␈↓␈α∩is␈α∪a␈α∩total
␈↓predicate;␈α
it␈α
is␈α
defined␈α
for␈α
all␈α
arguments.
␈↓␈↓↓3.1␈↓ 	↑Introduction     19␈↓


␈↓When␈α∂writing␈α∂or␈α∞reading␈α∂LISP␈α∂definitions␈α∞pay␈α∂particular␈α∂attention␈α∞to␈α∂the␈α∂domain␈α∂of␈α∞definition.
␈↓The␈α
following␈α
general␈α
hints␈α
should␈α
be␈α
useful:

␈↓␈↓↓1.␈↓␈α
Is␈α
it␈α
a␈α
function␈α
or␈α
predicate?

␈↓␈↓↓2␈↓.␈α
 Are␈α
there␈α
any␈α
restrictions␈α
on␈α
the␈α
argument␈α
positions?

␈↓␈↓↓3␈↓.␈α∂ Are␈α∂the␈α∂termination␈α⊂conditions␈α∂compatible␈α∂with␈α∂the␈α∂restrictions␈α⊂on␈α∂the␈α∂arguments.␈α∂ If␈α∂it␈α⊂is␈α∂a
␈↓recursion␈α
on␈αlists,␈α
check␈αfor␈α
the␈α
empty␈αlist;␈α
if␈αit␈α
is␈αa␈α
recursion␈α
of␈αarbitrary␈α
sexprs,␈αthen␈α
check␈αfor␈α
the
␈↓appearance␈α
of␈α
an␈α
atom.

␈↓␈↓↓4␈↓.␈α Whenever␈αa␈αfunction␈αcall␈αis␈αmade␈αwithin␈αthe␈αdefinition␈αare␈αall␈αthe␈αrestrictions␈αon␈αthat␈αfunction
␈↓satisfied.

␈↓␈↓↓5.␈↓␈α
Don't␈α
try␈α
to␈α
do␈α
too␈α
much.␈α
Be␈α
lazy.␈α∞There␈α
is␈α
usually␈α
a␈α
very␈α
simple␈α
termination␈α
case,␈α
and␈α∞if␈α
the
␈↓gereral␈αcase␈αlooks␈αmessy,␈αthen␈αwrite␈αsome␈αsubfunctions.␈α If␈αthe␈αtermination␈αcase␈αlooks␈αmessy␈αthere␈αis
␈↓probably␈α
something␈α
wrong.

␈↓Here␈α
are␈α
some␈α
more␈α
examples␈α
of␈α
LISP␈α
definitions.



␈↓␈↓ ¬⊂␈↓↓3.2  Examples of definitions␈↓


␈↓First␈αlet's␈αwrite␈αa␈α
predicate,␈α␈↓αmember␈↓␈αof␈αtwo␈α
arguments,␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓.␈α ␈↓αx␈↓␈α
is␈αto␈αbe␈αatomic;␈α
␈↓αy␈↓␈αis␈αto␈αbe␈α
a␈αlist;
␈↓␈↓αmember␈↓␈α
is␈α
to␈α
return␈α
␈↓αT␈↓␈α
just␈α
in␈α
the␈α
case␈α
that␈α
␈↓αx␈↓␈α
is␈α
an␈α
element␈α
of␈α
the␈α
list␈α
␈↓αy␈↓.

␈↓So␈α∞the␈α∞predicate␈α∂is␈α∞partial,␈α∞the␈α∂recursion␈α∞should␈α∞be␈α∂on␈α∞the␈α∞structure␈α∂of␈α∞␈↓αy␈↓,␈α∞and␈α∂termination␈α∞(with
␈↓value␈α␈↓αNIL␈↓)␈αshould␈αoccur␈αif␈α
␈↓αy␈↓␈αis␈αthe␈αempty␈αlist.␈αIf␈α
␈↓αy␈↓␈αis␈αnot␈αempty␈αthen␈αit␈α
has␈αa␈αfirst␈αelement,␈αcall␈αit␈α
␈↓αz␈↓;
␈↓compare␈α␈↓αz␈↓␈αwith␈α
␈↓αx␈↓.␈α If␈αthese␈α
elements␈αare␈αidentical␈αthen␈α
␈↓αmember␈↓␈αshould␈αreturn␈α
␈↓αT␈↓;␈αotherwise␈αsee␈α
if␈α␈↓αx␈↓
␈↓occurs␈α
in␈α
the␈α
remainder␈α
of␈α
the␈α
list␈α
␈↓αy␈↓.

␈↓Notes:

␈↓␈↓↓1.␈↓␈αWe␈α
cannot␈αuse␈α␈↓αeq␈↓␈α
to␈αcheck␈αequality␈α
since,␈αthough␈α
␈↓αx␈↓␈αis␈αatomic,␈α
there␈αis␈αno␈α
reason␈αto␈α
believe␈αthat
␈↓the␈α
elements␈α
of␈α
␈↓αy␈↓␈α
need␈α
be␈α
atomic.

␈↓␈↓↓2.␈↓␈α
We␈α
can␈α
get␈α
the␈α
first␈α
element␈α
of␈α
a␈α
list␈α
with␈α
␈↓αcar␈↓,␈α
and␈α
the␈α
rest␈α
of␈α
a␈α
list␈α
with␈α
␈↓αcdr␈↓.

␈↓So␈α
here's␈α
␈↓αmember␈↓:
␈↓α␈↓ β8member[x;y] <=␈↓ ¬8[null[y] → NIL;
␈↓α␈↓ β8␈↓ ¬8 equal[car[y];x] → T;
␈↓α␈↓ β8␈↓ ¬8 T → member[x;cdr[y]]]
␈↓␈↓↓20  Applications and Examples␈↓ 53.2␈↓


␈↓Let␈αus␈αnow␈αconsider␈αa␈αslightly␈αmore␈αcomplicated␈αnumerical␈αexample,␈αthe␈αfibonacci␈αsequence:␈α0,␈α1,␈α1,
␈↓2,␈α
3,␈α
5,␈α
8,␈α
...␈α
.␈α
This␈α
sequence␈α
is␈α
frequently␈α
characterized␈α
by␈α
the␈α
following␈α
recurrence␈α
relation:
␈↓␈↓ ε f(0) = 0
␈↓␈↓ ε f(1) = 1
␈↓␈↓ ¬Wf(n) = f(n-1)+f(n-2);

␈↓The␈α
translation␈α
to␈α
a␈α
LISP␈α
function␈α
is␈α
easy:
␈↓α␈↓ β8fib[n] <=␈↓ ∧X[eq[n;0] → 0;
␈↓α␈↓ β8␈↓ ∧X eq[n;1] → 1;
␈↓α␈↓ β8␈↓ ∧X T → +[fib[n-1];fib[n-2]]]

␈↓A␈α∞few␈α∞points␈α
can␈α∞be␈α∞made␈α∞here.␈α
First,␈α∞notice␈α∞that␈α∞the␈α
intuitive␈α∞evaluation␈α∞scheme␈α∞requires␈α
many
␈↓duplications␈αof␈αcomputation.␈α For␈α
example,␈αcomputation␈αof␈α␈↓αfib[5]␈↓␈α
requires␈αthe␈αcomputation␈αof␈α
␈↓αfib[4]␈↓
␈↓and␈α
␈↓αfib[3]␈↓.␈α But␈α
within␈αthe␈α
calculation␈α
of␈α␈↓αfib[4]␈↓␈α
we␈αmust␈α
again␈α
calculate␈α␈↓αfib[3]␈↓,␈α
etc.␈α It␈α
would␈αbe␈α
nice
␈↓if␈α∂we␈α∂could␈α∞restructure␈α∂the␈α∂definition␈α∂of␈α∞the␈α∂function,␈α∂␈↓αfib␈↓␈α∂to␈α∞stop␈α∂this␈α∂duplication␈α∂of␈α∞calculation.
␈↓Since␈αwe␈α␈↓↓do␈↓␈α
wish␈αto␈αrun␈α
programs␈αon␈αa␈αmachine␈α
we␈αshould␈αgive␈α
some␈αattention␈αto␈α
efficiency.␈α To
␈↓those␈α∀with␈α∀programming␈α∀experience,␈α∀the␈α∀solution␈α∀is␈α∀easy:␈α∀assign␈α∀the␈α∀partial␈α∀computations␈α∪to
␈↓temporary␈α∀variables.␈α∪ The␈α∀problem␈α∀here␈α∪is␈α∀that␈α∪our␈α∀current␈α∀subset␈α∪of␈α∀LISP␈α∀doesn't␈α∪contain
␈↓assignment.␈α
There␈α
is␈α
however␈α
a␈α
very␈α
useful␈α
trick␈α
which␈α
we␈α
can␈α
use.

␈↓We␈αwill␈αdefine␈αanother␈αfunction,␈αcalled␈α␈↓αfib*␈↓,␈αon␈αthree␈αvariables␈α␈↓αx␈↓,␈α␈↓αy␈↓,␈αand␈α␈↓αn␈↓.␈αThe␈αvariables,␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓,
␈↓will␈α
be␈α
used␈α
to␈α
carry␈α
the␈α
partial␈α
computations.␈α
Consider:
␈↓α␈↓ ¬Jfib␈↓β1␈↓α[n] <= fib*[n;0;1]

␈↓α␈↓ βHfib*[n;x;y] <=␈↓ ¬X[eq[n;0] → x;
␈↓α␈↓ βH␈↓ ¬X T → fib*[n-1;+[x;y];x]]

␈↓This␈αexample␈αis␈α
complicated␈αenough␈αto␈αwarrant␈α
examination.␈αThe␈αinitial␈α
call,␈α␈↓αfib␈↓β1␈↓α[n]␈↓,␈αhas␈αthe␈α
effect
␈↓of␈α
calling␈α
␈↓αfib*␈↓␈α
with␈α
␈↓αx␈↓␈α
initialized␈α
to␈α
␈↓α0␈↓␈α
and␈α
with␈α
␈↓αy␈↓␈α
initialized␈α
to␈α
␈↓α1␈↓.␈α
The␈α
calls␈α
on␈α
␈↓αfib*␈↓␈α
within␈α
the␈α
body␈α
of
␈↓the␈αdefinition,␈αsay␈αthe␈αi␈↓πth␈↓␈αsuch␈αrecursive␈αcall,␈αhas␈αthe␈αeffect␈αof␈αsaving␈αthe␈αi␈↓πth␈↓␈αfibonacci␈αnumber␈αin␈α␈↓αx␈↓
␈↓and␈α
the␈α
i-1␈↓πst␈↓␈α
in␈α
␈↓αy␈↓.␈α
 For␈α
example:
␈↓α␈↓ ∧Hfib␈↓β1␈↓α[4]␈↓ ελ= fib*[4;0;1]
␈↓α␈↓ ∧H␈↓ ελ= fib*[3;1;0]
␈↓α␈↓ ∧H␈↓ ελ= fib*[2;1;1]
␈↓α␈↓ ∧H␈↓ ελ= fib*[1;2;1]
␈↓α␈↓ ∧H␈↓ ελ= fib*[0;3;2]
␈↓α␈↓ ∧H␈↓ ελ= 3

␈↓This␈αsame␈αtrick␈αof␈αusing␈αauxiliary␈αfunctions␈αcan␈αbe␈αapplied␈αto␈αthe␈αfactorial␈αexample.␈αThe␈αresulting
␈↓definition␈αwill␈αbe␈αmore␈α
efficient,␈αthough␈αthe␈αgain␈α
in␈αefficiency␈αis␈αnot␈α
as␈αtransparent␈αas␈αthat␈α
in␈αthe
␈↓fibonacci␈α
example.
␈↓α␈↓ ¬Ifact␈↓β1␈↓α[n] <= fac*[n;1];

␈↓α␈↓ ∧#fac*[n;x] <= [eq[n;0] → x; T → fac*[n-1;*[n;x]]]
␈↓␈↓↓3.2␈↓ λ@Examples of definitions     21␈↓


␈↓It␈αis␈αclear␈α
in␈αthese␈αexamples␈α
that␈αthe␈αfunctions␈α␈↓αfact,␈α
fact␈↓β1␈↓␈αand␈α␈↓αfib,␈α
fib␈↓β1␈↓␈αare␈αequivalent.␈α
Perhaps␈αwe
␈↓should␈α
prove␈α∞that␈α
this␈α
is␈α∞so.␈α
 We␈α∞shall␈α
examine␈α
the␈α∞question␈α
of␈α
proofs␈α∞of␈α
equivalence␈α∞in␈α
Section
␈↓3.10.

␈↓The␈α
trick␈α
of␈α
auxiliary␈α
functions␈α
is␈α
clearly␈α
applicable␈α
to␈α
LISP␈α
functions␈α
defined␈α
over␈α
sexprs:
␈↓α␈↓ ∧∂length[n] <= [null[n] → 0; T → +[1;length[cdr[n]]]]

␈↓α␈↓ ¬'length␈↓β1␈↓α[n] <= length*[n;0]

␈↓α␈↓ βxlength*[n;x] <= [null[n] → x; T → length*[cdr[n];x+1]]
␈↓α␈↓and it seems apparent that:
␈↓␈↓α␈↓ ¬Blength[n] ≡ length␈↓β1␈↓α[n]

␈↓So␈αfar␈αour␈αexamples␈αhave␈αeither␈αbeen␈αnumerical␈αor␈αhave␈αbeen␈αpredicates.␈α Predicates␈αonly␈αrequire
␈↓traversing␈α
existing␈α
sexprs;␈α
certainly␈α
we␈α
will␈α
want␈α
to␈α
write␈α
algorithms␈α
to␈α
build␈α
new␈α
sexprs.␈α
 Consider:

␈↓α␈↓ ¬+reverse[x] <= rev*[x;NIL]

␈↓α␈↓ βgrev*[x;y] <= [null[x] → y; T → rev*[cdr[x];cons[car[x];y]]]

␈↓The␈αfunction,␈α␈↓αreverse␈↓,␈αbuilds␈αa␈αlist␈αwhich␈αis␈αthe␈αreverse␈αof␈αits␈αargument.␈α Notice␈αthat␈αthis␈αdefinition
␈↓uses␈α
an␈α
auxiliary␈α
function.␈α
 Sometimes␈α
it␈α
is␈α
more␈α
natural␈α
to␈α
express␈α
algorithms␈α
this␈α
way.␈α∞We␈α
will
␈↓see␈α
a␈α
"direct"␈α
definition␈α
of␈α
the␈α
reversing␈α
function␈α
in␈α
a␈α
moment.

␈↓This␈α∩␈↓αreverse␈↓␈α∩function␈α∩builds␈α⊃up␈α∩the␈α∩new␈α∩list␈α∩in␈α⊃a␈α∩very␈α∩straightforward␈α∩mannner,␈α∩␈↓αcons␈↓ing␈α⊃the
␈↓elements␈αonto␈αthe␈αsecond␈αargument␈αof␈α␈↓αrev*␈↓.␈αSince␈α␈↓αy␈↓␈αwas␈αinitialized␈αto␈α␈↓αNIL␈↓␈αwe␈αare␈αassured␈αthat␈αthe
␈↓resulting␈α
construct␈α
will␈α
be␈α
a␈α
list.

␈↓Construction␈α
is␈α∞usually␈α
not␈α∞quite␈α
so␈α∞straightforward.␈α
Suppose␈α
we␈α∞wish␈α
to␈α∞define␈α
a␈α∞LISP␈α
function
␈↓named␈α␈↓αappend␈↓␈αof␈αtwo␈αlist␈αarguments,␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓,␈αwhich␈αis␈αto␈αreturn␈αa␈αnew␈αlist␈αwhich␈αhas␈α␈↓αx␈↓␈αappended
␈↓onto␈α
the␈α
front␈α
of␈α
␈↓αy␈↓.␈α
 For␈α
example:

␈↓α␈↓ ∧Sappend[(A B D);(C E)] = (A B D C E)
␈↓α␈↓ ∧'append[A;(B C)] ␈↓ is undefined␈↓α. A is not a list.
␈↓α␈↓ βZappend[(A B C);NIL] = append[NIL;(A B C)] = (A B C)

␈↓So␈α∩␈↓αappend␈↓␈α∪is␈α∩a␈α∪partial␈α∩function.␈α∩It␈α∪should␈α∩be␈α∪defined␈α∩by␈α∩recursion,␈α∪but␈α∩recursion␈α∪on␈α∩which
␈↓argument?␈α Well,␈αif␈αeither␈αargument␈αis␈α␈↓αNIL␈↓␈αthe␈αthe␈αvalue␈αis␈αthe␈αother␈αargument.␈αThe␈αnext␈α
simplest
␈↓case␈αis␈αa␈αone-element␈αlist;␈αif␈αexactly␈αone␈αof␈α␈↓αx␈↓␈αor␈α␈↓αy␈↓␈αis␈αa␈αsingleton␈αhow␈αdoes␈αthat␈αhelp␈αus␈αdiscover␈αthe
␈↓recurrence␈αrelation␈αfor␈αappending?␈αIt␈αdoesn't␈αhelp␈αmuch␈αif␈α␈↓αy␈↓␈αis␈αa␈αsingleton;␈αbut␈αif␈α␈↓αx␈↓␈αis,␈αthen␈α␈↓αappend␈↓
␈↓could␈α
give:

␈↓␈↓ ¬?␈↓αcons[car[x];y]␈↓ as result. 

␈↓So␈α
recursion␈α
on␈α
␈↓αx␈↓␈α
is␈α
likely.␈α
The␈α
definition␈α
follows␈α
easily␈α
now.
␈↓␈↓↓22  Applications and Examples␈↓ 53.2␈↓


␈↓␈↓ β8␈↓αappend[x;y] <= [null[x] → y; T → cons[car[x];append[cdr[x];y]]].␈↓ 

␈↓Notice␈αthat␈αthe␈αconstruction␈αof␈αthe␈αresult␈αis␈α
a␈αbit␈αmore␈αobscure␈αthan␈αthat␈αinvolved␈αin␈α
␈↓αreverse␈↓.␈αThe
␈↓construction␈α
has␈α
to␈α
"wait"␈α
until␈α
we␈α
have␈α
seen␈α
the␈α
end␈α
of␈α
the␈α
list␈α
␈↓αx␈↓.␈α
For␈α
example:
␈↓α␈↓ αXappend[(A B C);(D E F)]␈↓ ε8= cons[A;append[(B C);(D E F)]]
␈↓α␈↓ αX␈↓ ε8= cons[A;cons[B;append[(C);(D E F)]]]
␈↓α␈↓ αX␈↓ ε8= cons[A;cons[B;cons[C;append[NIL;(D E F)]]]]
␈↓α␈↓ αX␈↓ ε8= cons[A;cons[B;cons[C;(D E F)]]]
␈↓α␈↓ αX␈↓ ε8= cons[A;cons[B;(C D E F)]]
␈↓α␈↓ αX␈↓ ε8= cons[A;(B C D E F)]
␈↓α␈↓ αX␈↓ ε8= (A B C D E F)

␈↓We␈α
are␈αassured␈α
of␈α
constructing␈αa␈α
list␈αhere␈α
because␈α
␈↓αy␈↓␈αis␈α
a␈α
list␈αand␈α
we␈αare␈α
␈↓αcons␈↓ing␈α
onto␈αthe␈α
front␈αof␈α
it.
␈↓LISP␈αfunctions␈αwhich␈αare␈αto␈αconstruct␈αlist-output␈αby␈α
␈↓αcons␈↓ing␈α␈↓↓must␈↓␈α␈↓αcons␈↓␈αonto␈αthe␈αfront␈αof␈αof␈α
a␈α␈↓↓list␈↓.
␈↓That␈αlist␈αmay␈αbe␈αeither␈αnon␈αempty␈αor␈αthe␈αempty␈αlist,␈α␈↓αNIL␈↓.␈αThis␈αis␈αwhy␈αthe␈α
termination␈αconditions
␈↓on␈α⊃such␈α⊃list-constructing␈α⊃functions␈α⊃return␈α⊃␈↓αNIL␈↓.␈α⊃ Compare␈α⊃␈↓αreverse␈↓␈α⊃on␈α⊃page␈α⊃21␈α⊃or␈α⊃the␈α⊂following
␈↓binary␈α
function,␈α
␈↓αdotem␈↓.␈α
 The␈α
arguments␈α
to␈α
␈↓αdotem␈↓␈αare␈α
both␈α
lists␈α
assumed␈α
to␈α
contain␈α
the␈αsame␈α
number
␈↓of␈α∞elements.␈α∞The␈α∞value␈α
returned␈α∞is␈α∞to␈α∞be␈α∞a␈α
list␈α∞of␈α∞dotted␈α∞pairs;␈α∞the␈α
elements␈α∞of␈α∞the␈α∞pairs␈α∞are␈α
the
␈↓corresponding␈α
elements␈α
of␈α
the␈α
input␈α
lists.␈α
Thus:

␈↓αdotem[x;y] <= [␈↓ β8null[x] → NIL;
␈↓α␈↓ β8T → cons[cons[car[x];car[y]];dotem[cdr[x];cdr[y]]]]]


␈↓Now,␈α
as␈α
promised␈α
on␈α
page␈α
21,␈α
here␈α
is␈α
a␈α
"direct"␈α
definition␈α
of␈α
␈↓αreverse␈↓.

␈↓αreverse[x] <=␈↓ βλ[null[x] → NIL;
␈↓α␈↓ βλ T → append[reverse[cdr[x]];cons[car[x];NIL]]]

␈↓This␈α
reversing␈α
function␈α
is␈α
not␈α
as␈α
efficient␈α
as␈α
the␈α
previous␈α
one.

␈↓It␈α∩␈↓↓is␈↓␈α∩possible␈α∩to␈α∩write␈α∩a␈α∩directly␈α∩recursive␈α∩reversing␈α∩function␈α∩with␈α∩no␈α∩auxiliary␈α∪functions,␈α∩no
␈↓functions␈αother␈α
than␈αthe␈αprimitives,␈α
and␈αno␈αefficiency.␈α
We␈αshall␈αdo␈α
so␈αsimply␈αbecause␈α
it␈αis␈α
a␈αgood
␈↓example␈αof␈αthe␈αprocess␈αof␈αdiscovering␈αthe␈αgeneral␈αcase␈αof␈αthe␈αrecursion␈αby␈αcareful␈αconsideration␈αof
␈↓examples.␈α
Let␈α
us␈α
call␈α
the␈α
function␈α
␈↓αrev␈↓.

␈↓Let's␈α⊂worry␈α⊂about␈α⊂the␈α⊂termination␈α⊂conditions␈α⊂later.␈α⊂Consider,␈α⊂for␈α⊂example,␈α⊂␈↓αrev[(A B C D)]␈↓.␈α∂This
␈↓should␈α⊂evaluate␈α⊂to␈α⊂␈↓α(D C B A)␈↓.␈α∂How␈α⊂can␈α⊂we␈α⊂construct␈α∂this␈α⊂list␈α⊂by␈α⊂recursive␈α∂calls␈α⊂on␈α⊂␈↓αrev␈↓?␈α⊂In␈α∂the
␈↓following,␈α_assume␈α_␈↓αx␈↓␈α_is␈α↔bound␈α_to␈α_␈↓α(A B C D)␈↓.␈α_ Now␈α_note␈α↔that␈α_␈↓α(D C B A)␈↓␈α_is␈α_the␈α_value␈α↔of
␈↓␈↓αcons[D;(C B A)]␈↓.␈αThen␈α␈↓αD␈↓␈αis␈α␈↓αcar[rev[cdr[x]]]␈↓␈α(it␈αis␈αalso␈α␈↓αcar[rev[x]]␈↓␈αbut␈αthat␈αwould␈αnot␈αhelp␈αus).␈α How
␈↓can␈α
we␈α
get␈α
␈↓α(C B A)␈↓?  ␈α
Well:
␈↓␈↓↓3.2␈↓ λ@Examples of definitions     23␈↓



␈↓α␈↓ ∧λ(C B A)␈↓ ¬_= rev[(A B C)]
␈↓α␈↓ ∧λ␈↓ ¬_= rev[cons[A;(B C)]]   ␈↓(we are going after ␈↓αcdr[x]␈↓ again)␈↓α
␈↓α␈↓ ∧λ␈↓ ¬_                        ␈↓but first  we can get ␈↓αA␈↓ from ␈↓αx.
␈↓α␈↓ ∧λ␈↓ ¬_= rev[cons[car[x];(B C)]]
␈↓α␈↓ ∧λ␈↓ ¬_= rev[cons[car[x];rev[(C B)]]]
␈↓α␈↓ ∧λ␈↓ ¬_= rev[cons[car[x];rev[cdr[(D C B)]]]]
␈↓α␈↓ ∧λ␈↓ ¬_= rev[cons[car[x];rev[cdr[rev[cdr[x]]]]]]

␈↓α␈↓Finally␈↓α
␈↓α␈↓ β*rev[x] = cons[car[rev[cdr[x]]];rev[cons[car[x];rev[cdr[rev[cdr[x]]]]]]]

␈↓The␈α∞termination␈α∂conditions␈α∞are␈α∂simple.␈α∞First␈α∂␈↓αrev[NIL]␈↓␈α∞gives␈α∂␈↓αNIL␈↓.␈α∞ Then␈α∂notice␈α∞that␈α∂the␈α∞general
␈↓case␈α
which␈α
we␈α
just␈α
constructed␈αhas␈α
␈↓↓two␈↓␈α
␈↓αcons␈↓es.␈α
 That␈α
means␈α
the␈αshortest␈α
list␈α
which␈α
it␈α
can␈α
make␈αis␈α
of
␈↓length␈αtwo.␈α So␈αlists␈αof␈αlength␈αone␈αare␈αhandled␈αseparately:␈αthe␈αreverse␈αof␈αsuch␈αa␈αlist␈αis␈αitself.␈α Thus
␈↓the␈α
complete␈α
definition␈α
should␈α
be:

␈↓αrev[x] <= [␈↓ βλnull[x] → NIL;
␈↓α␈↓ βλnull[cdr[x]] → x;
␈↓α␈↓ βλT → cons[car[rev[cdr[x]]];rev[cons[car[x];rev[cdr[rev[cdr[x]]]]]]]]



␈↓␈↓ ¬v␈↓↓3.3  Problems␈↓

␈↓I  Use the following definition:
␈↓α␈↓ αXmatch[k;m] <=␈↓ ∧([null[k] → NO;
␈↓α␈↓ αX␈↓ ∧( null[m] → NO;
␈↓α␈↓ αX␈↓ ∧( eq[car[k];car[m]] → car[k];
␈↓α␈↓ αX␈↓ ∧( T → match[cdr[k];cdr[m]]]

␈↓and evaluate:
␈↓␈↓↓1.␈↓α match[(X);(X)]   ␈↓↓2.␈↓α match[(A B E);(J O E)]  ␈↓↓3.␈↓α match[(F O O); (BAZ)]
␈↓␈↓↓24  Applications and Examples␈↓ 53.3␈↓



␈↓II  Now write your own.

␈↓␈↓↓1.␈↓α␈α
among[x;y]␈α
<=␈α
...␈α
:␈α
among␈↓␈α
is␈αto␈α
be␈α
a␈α
predicate;␈α
␈↓αx␈↓␈α
is␈α
an␈α
atom;␈α␈↓αy␈↓␈α
is␈α
a␈α
list␈α
of␈α
atoms.␈α
 ␈↓αamong␈↓␈α
is␈αto␈α
return
␈↓␈↓ α8␈↓αNIL␈↓␈α
if␈α
␈↓αx␈↓␈α
is␈α
not␈α
found␈α
as␈α
an␈α
element␈α
of␈α
␈↓αy␈↓;␈α
o.w.␈α
 among␈α
is␈α
to␈α
return␈α
␈↓αT␈↓.

␈↓␈↓ αXe.g. ␈↓αamong[A;(A B C)] = among[A;(C D E A)] = T
␈↓α␈↓ αX     among[A1;(A2 B2)] = NIL.


␈↓␈↓↓2.␈↓α␈αanywhere[x;y]␈α<=␈α...␈α:␈αanywhere␈↓␈αis␈αa␈αpredicate;␈α␈↓αx␈↓␈α
is␈αan␈αatom;␈α␈↓αy␈↓␈αis␈αan␈αarbitrary␈αsexpr.␈α␈↓αanywhere␈↓␈αis␈α
to
␈↓␈↓ α8return␈α
␈↓αT␈↓␈α
just␈α
in␈α
the␈α
case␈α
that␈α
␈↓αx␈↓␈α
appears␈α
somewhere␈α
in␈α
␈↓αy␈↓.

␈↓␈↓ αXe.g. ␈↓αanywhere[A;(A B C)] = anywhere[A;((A . B). C)] = T
␈↓α␈↓ αX     anywhere[A;(B C D)] = NIL.


␈↓␈↓↓3.␈↓α␈α∞collectpair[z;x;y]␈α
<=␈α∞...␈α∞:␈α
x␈↓␈α∞and␈α∞␈↓αy␈↓␈α
are␈α∞atoms;␈α∞␈↓αz␈↓␈α
is␈α∞an␈α∞sexpression,␈α
some␈α∞of␈α∞whose␈α
subexpressions,
␈↓␈↓ α8may␈α
begin␈α
␈↓α(x␈α
...)␈↓␈α
or␈α
␈↓α(y␈α
...)␈↓.␈α
 ␈↓αcollectpair␈↓␈α
is␈αto␈α
return␈α
a␈α
dotted␈α
pair␈α
whose␈α
␈↓αcar␈↓-part␈α
is␈α
a␈α
list␈αof␈α
all
␈↓␈↓ α8the␈α
occurrences␈α
of␈α
␈↓α(x...)␈↓␈α
and␈α
whose␈α
␈↓αcdr␈↓-part␈α
is␈α
a␈α
list␈α
of␈α
all␈α
occurrences␈α
of␈α
␈↓α(y␈α
...)␈↓.

␈↓␈↓ αXe.g. ␈↓αcollectpair[((A 1)((B . 2)(C A 4)));A;B] = (((A 1)(A 4)).((B . 2)))


␈↓␈↓↓4.␈↓α␈α⊃pred[x]␈α⊃<=␈α⊃...␈α⊃:␈α∩x␈↓␈α⊃is␈α⊃a␈α⊃positive␈α⊃integer.␈α⊃␈↓αpred␈↓␈α∩is␈α⊃a␈α⊃function,␈α⊃returning␈α⊃the␈α⊃predecessor␈α∩of␈α⊃its
␈↓␈↓ α8argument.

␈↓␈↓ αXe.g. ␈↓αpred[3] = 2;  pred[0] ␈↓is undefined␈↓α;
␈↓α␈↓ αX     pred[add1[x]] = x ␈↓ for ␈↓αx ␈↓≥␈↓α 0.␈↓




␈↓␈↓ ∧↑␈↓↓3.4  Examples of LISP applications␈↓


␈↓The␈α⊃crucial␈α⊃problems␈α⊃in␈α⊃data␈α⊃structures␈α⊃are␈α⊃the␈α⊃interrelationships␈α⊃between␈α⊃algorithms␈α⊃and␈α⊂the
␈↓representation␈αwhich␈αis␈αchosen.␈α
 Once␈αthese␈αchoices␈αhave␈αbeen␈α
made␈αthen␈αthe␈αrest␈αis␈α
easy.␈α Knuth
␈↓once␈αremarked␈α
that␈αsimulation␈αlanguages␈α
should␈αnot␈α
have␈αany␈αI/O␈α
statements,␈αbecause␈α
if␈αyou␈αgot␈α
to
␈↓the␈α
point␈αof␈α
running␈α
on␈αa␈α
machine␈α
then␈αyou␈α
have␈αdone␈α
something␈α
wrong.␈α All␈α
your␈α
ideas␈αshould
␈↓have␈αbeen␈αsorted␈αout␈αas␈α
you␈αwere␈αconstructing␈αthe␈αmodel␈α
and␈αdescribing␈αthe␈αalgorithms.␈α He␈α
wasn't
␈↓quite␈α
serious␈α
but␈α
the␈α
point␈α
should␈α
be␈α
clear.

␈↓The␈αnext␈αfew␈αsections␈αwill␈αexamine␈αsome␈αnon-trivial␈αproblems␈αin␈αnon-numerical␈αcomputation␈α(data
␈↓structures).␈α We␈α
will␈αdescribe␈α
the␈αproblem␈α
intuitively,␈αpick␈α
an␈αinitial␈α
representation␈αfor␈αthe␈α
problem,
␈↓␈↓↓3.4␈↓ π←Examples of LISP applications     25␈↓


␈↓write␈αthe␈αLISP␈αalgorithm,␈αand␈αin␈αsome␈αcases␈α"tune"␈αthe␈αalgorithm␈αby␈αpicking␈α"more␈αefficient"␈αdata
␈↓representations.␈α
 The␈α
examples␈α
share␈α
other␈α
important␈α
characteristics:

␈↓␈↓↓1.␈↓␈α
We␈α
take␈α
our␈α
intuitive␈α
algorithm␈α
and␈α
try␈α
to␈α
express␈α
it␈α
as␈α
a␈α
LISP␈α
function.

␈↓␈↓↓2.␈↓␈α
We␈α
take␈α
the␈α
intuitive␈α
domain␈α
and␈α
encode␈α
its␈α
elements␈α
as␈α
sexprs.

␈↓␈↓↓3.␈↓␈α
␈↓↓1␈↓␈α
and␈α
␈↓↓2␈↓␈α
usual␈α
happen␈α
in␈α
parallel␈α
and␈α
are␈α
perhaps␈α
iterated.

␈↓␈↓↓4.␈↓␈α
We␈α
evaluate␈α
the␈α
LISP␈α
function␈α
on␈α
a␈α
representation␈α
of␈α
a␈α
problem.

␈↓␈↓↓5.␈↓␈α
We␈α
interpret␈α
the␈α
sexpression␈α
output␈α
as␈α
an␈α
answer␈α
to␈α
our␈α
problem.

␈↓Pictorially:

␈↓intuitive algorithm => LISP function␈↓ ¬h|
␈↓␈↓ ¬h|  evaluation
␈↓␈↓ ¬h|==============> interpret sexpr output as answer
␈↓␈↓ ¬h|
␈↓domain of algorithm => sexpressions␈↓ ¬h|


␈↓This␈α
process␈α
is␈α
certainly␈α
not␈α
unique␈α
to␈α
LISP.␈α
Every␈α
language␈α
does␈α
it.



␈↓␈↓ ¬L␈↓↓3.5  Differentiation␈↓


␈↓This␈α∀example␈α∀will␈α∀describe␈α∀a␈α∀rudimentary␈α∀differentiation␈α∀routine␈α∀for␈α∀polynomials␈α∀in␈α∀several
␈↓variables.␈α
 First␈αyou␈α
should␈αrealize␈α
that␈αthe␈α
normal␈αdefinition␈α
of␈αdifferentiation␈α
is␈α
recursive!␈α The
␈↓question␈α⊃of␈α⊃differentiation␈α⊃of␈α⊃a␈α⊃sum␈α⊃is␈α⊃thrown␈α⊃back␈α⊃on␈α⊃the␈α⊃differentiation␈α⊃of␈α⊃each␈α⊃summand.
␈↓Similar␈α∪relationships␈α∪hold␈α∪for␈α∪products,␈α∪differences,␈α∪and␈α∪powers.␈α∪ As␈α∪with␈α∪all␈α∪good␈α∪recursive
␈↓definitions,␈αthere␈αmust␈αbe␈αsome␈αtermination␈αconditions.␈α What␈αare␈αthe␈αtermination␈αconditions␈αhere?
␈↓Differentiation␈α∞of␈α∞a␈α∞variable,␈α∞say␈α∞␈↓αx␈↓,␈α∞w.r.t.␈α∞to␈α∞␈↓αx␈↓␈α∞is␈α∞defined␈α∞to␈α∞be␈α∞1;␈α∞differentiating␈α∞a␈α∞constant␈α∞or␈α
a
␈↓variable␈α⊃not␈α⊂equal␈α⊃to␈α⊂␈↓αx␈↓␈α⊃with␈α⊃respect␈α⊂to␈α⊃␈↓αx␈↓␈α⊂gives␈α⊃a␈α⊂result␈α⊃of␈α⊃zero.␈α⊂ It␈α⊃is␈α⊂easy␈α⊃to␈α⊃write␈α⊂recursive
␈↓algorithms␈αin␈αLISP;␈αthe␈αonly␈αproblem␈αis␈αthat␈αthe␈αdomain␈α(and␈αrange)␈αof␈αLISP␈αfunctions␈αis␈αSexprs,
␈↓not␈αthe␈αpolynomials␈αwhich␈αwe␈αneed.␈α Problem:␈αrepresent␈αarbitrary␈αpolynomials␈αas␈αSexprs.␈α Though
␈↓polynominals␈α∂can␈α∂be␈α∂arbitrarily␈α∂complex,␈α∂involving␈α∂plus,␈α∂times,␈α∂minus,␈α∂powers,␈α∂etc.␈α∂their␈α∂general
␈↓format␈α
is␈αvery␈α
simple␈αif␈α
they␈α
are␈αdescribed␈α
in␈αprefix␈α
notation␈α
(standard␈αfunctional␈α
notation)␈αand␈α
we
␈↓assume␈α
that␈α
+,␈α
*,␈α
-␈α
and␈α
**␈α
are␈α
binary␈α
operations.␈α
 For␈α
example:

␈↓␈↓ ¬_␈↓↓infix␈↓ πλprefix

␈↓α␈↓ ¬_x*z+2y␈↓ πλ+[*[x,z], *[2,y]]
␈↓α␈↓ ¬_x*y*z␈↓ πλ*[x,*[y,z]]
␈↓␈↓↓26  Applications and Examples␈↓ 43.5␈↓


␈↓(optional␈α
problem:␈α
write␈α
BNF␈α
equations␈α
expressing␈α
such␈α
a␈α
representation␈α
of␈α
polynominals)

␈↓How␈αdoes␈αthis␈α
help␈αus;␈αwe␈α
still␈αdon't␈αhave␈αSexprs?␈α
 First␈αthe␈αoperations␈α
need␈αto␈αbe␈α
represented␈αas
␈↓atoms:
␈↓␈↓ ¬_␈↓↓operation␈↓ πλrepresentation

␈↓α␈↓ ¬_+␈↓ πλPLUS
␈↓α␈↓ ¬_-␈↓ πλMINUS
␈↓α␈↓ ¬_*␈↓ πλTIMES
␈↓α␈↓ ¬_**␈↓ πλEXPT

␈↓How␈α⊂about␈α∂a␈α⊂representation␈α⊂of␈α∂variables␈α⊂and␈α⊂constants?␈α∂ Let␈α⊂the␈α⊂variables␈α∂be␈α⊂mapped␈α⊂to␈α∂their
␈↓uppercase␈α∞counterpart,␈α
(a␈α∞decent␈α∞atom);␈α
and␈α∞let␈α
constants␈α∞(numbers),␈α∞be␈α
just␈α∞numbers,␈α∞also␈α
decent
␈↓atoms.␈α Looking␈αahead␈αto␈α
the␈αalgorithm,␈αthe␈αtermination␈α
condition␈αon␈αvariables␈αand␈α
constants␈αcan
␈↓then␈α
just␈α
be␈α
given␈α
in␈α
terms␈α
of␈α
the␈α
predicate,␈α
␈↓αatom␈↓.

␈↓That␈α
is,if␈α
␈↓αu␈↓␈α
is␈α
a␈α
constant␈α
or␈α
a␈α
variable␈α
then:
␈↓␈↓ αXD␈↓αu␈↓/D␈↓αx␈↓ = 1␈↓ βxif ␈↓αx = u
␈↓α␈↓ αX␈↓ βx␈↓0 otherwise

␈↓will translate as :

␈↓␈↓ αX␈↓α[atom[u] → [eq[u;x] → 1; T → 0] ....␈↓

␈↓Now␈α
finally,␈α
how␈α
can␈α
we␈α
represent␈α
an␈α
arbitrary␈α
polynomial␈α
as␈α
an␈α
sexpr.

␈↓Write:
␈↓␈↓ ¬_+[x,y]␈↓ πλas ␈↓α(PLUS X Y)␈↓
␈↓␈↓ ¬_*[x,y]␈↓ πλas ␈↓α(TIMES X Y)␈↓
␈↓␈↓ ¬_**[x,y]␈↓ πλas ␈↓α(EXPT X Y)␈↓
␈↓or in general:
␈↓α␈↓ ∧(f[t␈↓β1␈↓α,t␈↓β2␈↓α]    ␈↓as ␈↓α(F␈↓, translate of ␈↓αt␈↓β1␈↓, translate of ␈↓αt␈↓β2␈↓α)

␈↓For example:

␈↓α␈↓ εβx␈↓π2␈↓α + 2yz + u
␈↓will be translated to the following prefix notation:

␈↓α␈↓ ¬.+[**[x,2], +[*[2,*[y,z]], u]]

␈↓(This␈αis␈α
messier␈αthan␈αit␈α
really␈αneeds␈α
to␈αbe␈αbecause␈α
we␈αrequired␈α
+␈αand␈α*␈α
to␈αbe␈α
binary).␈α From␈αthis␈α
it`s
␈↓easy␈α
to␈α
get␈α
the␈α
sexpr␈α
form:
␈↓α␈↓ βD(PLUS (EXPT X 2)  (PLUS (TIMES 2 (TIMES Y Z)) U))

␈↓Now␈α
we␈α
can␈α
complete␈α
the␈α
differentiation␈α
algorithm.␈α
 We␈α
know:
␈↓␈↓ αXD[␈↓αf + g␈↓]/D␈↓αx␈↓ = D␈↓αf/␈↓D␈↓αx + ␈↓D␈↓αg␈↓/D␈↓αx.
␈↓␈↓↓3.5␈↓ 	9Differentiation     27␈↓


␈↓We would see ␈↓αu = f + g␈↓ as ␈↓αu = (PLUS, ␈↓rep of ␈↓αf␈↓, rep of ␈↓αg)␈↓
␈↓Where:␈↓ αX␈↓αcadr[u] = ␈↓rep of ␈↓αf
␈↓α␈↓ αXcaddr[u] = ␈↓rep of ␈↓αg␈↓.        ␈↓π 3␈↓



␈↓The␈α
result␈α
of␈α
differentiation␈α
␈↓αu␈↓␈α
is␈α
to␈α
be␈α
a␈α
new␈α
list␈α
of␈α
three␈α
elements:

␈↓␈↓ αX1.  the symbol ␈↓αPLUS␈↓.
␈↓␈↓ αX2.  the effect of ␈↓αdiff␈↓ operating on the rep. of ␈↓αf␈↓.
␈↓␈↓ αX3.  the effect of ␈↓αdiff␈↓ operating on the rep. of ␈↓αg␈↓.

␈↓Thus another part of the algorithm:

␈↓α␈↓ αXeq [car[u];PLUS] →␈↓ ∧xlist [PLUS; diff[cadr[u]x];diff[caddr[u];x]]
␈↓α␈↓.

␈↓D[␈↓αf - g]/␈↓D␈↓αx␈↓ is very similar.

␈↓D[␈↓αf*g]␈↓/D␈↓αx␈↓ is defined to be ␈↓αf* ␈↓D␈↓αg␈↓/D␈↓αx + g *␈↓D␈↓αf/␈↓D␈↓αx␈↓.


␈↓So here's another part of ␈↓αdiff␈↓:
␈↓α␈↓ αXeq[car[u];TIMES] →␈↓ ∧xlist[PLUS;
␈↓α␈↓ αX␈↓ ∧x     list[TIMES; cadr[u];diff [caddr[u];x]];
␈↓α␈↓ αX␈↓ ∧x     list[TIMES;caddr[u];diff [cadr[u];x]]]

␈↓Here`s an example. We know:

␈↓␈↓ ¬ED[␈↓αx*y + x]␈↓/D␈↓αx = y + 1␈↓












________________
␈↓␈↓π 3␈↓␈α∃We␈α∀have␈α∃done␈α∃a␈α∀reasonably␈α∃evil␈α∀thing␈α∃here.␈α∃We␈α∀have␈α∃tied␈α∀the␈α∃algorithm␈α∃for␈α∀symbolic
␈↓differentiation␈α_to␈α↔a␈α_specific␈α_representation␈α↔for␈α_polynomials.␈α↔It␈α_is␈α_useful␈α↔to␈α_use␈α_a␈α↔specific
␈↓representation␈α∞for␈α∞the␈α∞moment␈α∞and␈α∞repent␈α∞later.␈α∂In␈α∞particular,␈α∞see␈α∞page␈α∞29,␈α∞page␈α∞33␈α∞and␈α∂page␈α∞159.
␈↓␈↓↓28  Applications and Examples␈↓ 43.5␈↓



␈↓Try:␈α
␈↓α
␈↓αdiff [PLUS (TIMES,X,Y) X); X]
␈↓α␈↓ αX=list [PLUS, diff [TIMES X Y) X]; diff [X;X]]
␈↓α␈↓ αX=list [PLUS;
␈↓α␈↓ αX␈↓ βHlist [PLUS;
␈↓α␈↓ αX␈↓ βH␈↓ ∧(list [TIMES; X; diff [Y;X]];
␈↓α␈↓ αX␈↓ βH␈↓ ∧(list [TIMES; Y; diff [X;X]]];
␈↓α␈↓ αX␈↓ βHdiff [X;X]]

␈↓α␈↓ αX=list [PLUS;
␈↓α␈↓ αX␈↓ βHlist [PLUS;
␈↓α␈↓ αX␈↓ βH␈↓ ∧(list [TIMES; X ;0];
␈↓α␈↓ αX␈↓ βH␈↓ ∧(list [TIMES; Y;1]];
␈↓α␈↓ αX␈↓ βH1 ]

␈↓α␈↓ αX=(PLUS  (PLUS (TIMES X 0) (TIMES Y 1)) 1)


␈↓α␈↓which␈α
can␈α
be␈α
interpreted␈α
as:

␈↓␈↓α␈↓ ¬qx*0 + y*1 + 1 . ␈↓ 

␈↓Which␈α
introduces␈α
another␈α
set␈α
of␈α
non-numerical␈α
algorithms:␈α
algebraic␈α
simplification!
␈↓↓␈↓ ¬nPoints to note.

␈↓This␈αproblem␈αof␈αrepresentation␈αis␈αtypical␈αof␈αdata␈αstructure␈αalgorithms␈α(regardless␈αof␈αwhat␈αlanguage
␈↓you␈αuse).␈α
 That␈αis␈α
once␈αyou␈α
have␈αdecided␈α
what␈αthe␈α
intuitive␈αproblem␈α
is␈αpick␈α
a␈αrepresentation␈α
which
␈↓makes␈αyour␈αalgorithms␈αclean␈α(then␈αworry␈αabout␈αefficiency).␈α In␈αthe␈αnext␈αset␈αof␈αexamples␈αwe␈αwill␈αsee
␈↓a␈α∩series␈α⊃of␈α∩representation␈α∩each␈α⊃becoming␈α∩more␈α⊃and␈α∩more␈α∩"efficient"␈α⊃and␈α∩each␈α∩requiring␈α⊃more
␈↓"knowledge"␈α
being␈α
built␈α
into␈α
the␈α
algorithm.

␈↓Here's␈α
the␈α
whole␈α
algorithm␈α
for␈α
differentiation␈α
using␈α
+␈α
and␈α
*.␈α
 ␈↓α
␈↓␈↓↓3.5␈↓ 	9Differentiation     29␈↓



␈↓αdiff[u;x] <=
␈↓α␈↓ αX[atom [u] → [eq [x,u] → 1; T → 0];
␈↓α␈↓ αX eq [car [u]; PLUS] → list␈↓ ¬h[PLUS;
␈↓α␈↓ αX␈↓ ¬h diff [cadr [u]; x];
␈↓α␈↓ αX␈↓ ¬h diff [caddr [u]; x]]
␈↓α␈↓ αX eq [car [u]; TIMES] → list␈↓ ¬h[PLUS;
␈↓α␈↓ αX␈↓ ¬h list␈↓ εH[TIMES;
␈↓α␈↓ αX␈↓ ¬h␈↓ εH cadr [u];
␈↓α␈↓ αX␈↓ ¬h␈↓ εH diff [caddr [u]; x]];
␈↓α␈↓ αX␈↓ ¬h list␈↓ εH[TIMES;
␈↓α␈↓ αX␈↓ ¬h␈↓ εH caddr [u];
␈↓α␈↓ αX␈↓ ¬h␈↓ εH diff [cadr [u]; x]];
␈↓α␈↓ αX T → LOSER]

␈↓As␈α⊂we␈α⊂mentioned␈α⊂earlier,␈α⊂the␈α⊂current␈α⊂manifestation␈α∂of␈α⊂␈↓αdiff␈↓␈α⊂encodes␈α⊂too␈α⊂much␈α⊂of␈α⊂our␈α∂particular
␈↓representation␈αfor␈αpolynomials.␈αThe␈αseparation␈αof␈αalgorithm␈αfrom␈αrepresentation␈αis␈αbeneficial␈αfrom
␈↓at␈α∩least␈α∩two␈α∩standpoints.␈α∩ First,␈α∩changing␈α⊃representation␈α∩should␈α∩have␈α∩a␈α∩minimal␈α∩effect␈α∩on␈α⊃the
␈↓structure␈α∩of␈α⊃the␈α∩algorithm.␈α⊃␈↓αdiff␈↓␈α∩␈↓↓knows␈↓␈α⊃that␈α∩variables␈α⊃are␈α∩represented␈α⊃as␈α∩atoms␈α⊃and␈α∩a␈α∩sum␈α⊃is
␈↓repesented␈α
as␈α
a␈α
list␈α
whose␈α
␈↓αcar␈↓-part␈α
is␈α
␈↓αPLUS␈↓.␈α
 Second,␈α
readability␈α
suffers␈α
greatly.

␈↓How␈α∩much␈α∩of␈α∩␈↓αdiff␈↓␈α∩really␈α∩needs␈α∩to␈α∪know␈α∩about␈α∩the␈α∩representation␈α∩and␈α∩how␈α∩can␈α∪we␈α∩improve
␈↓readability?

␈↓First␈αthe␈α␈↓αcar-cdr␈↓␈αchains␈αare␈αnot␈αparticularly␈αmnemonic.␈αWe␈αused␈α␈↓αcadr␈↓␈αto␈αget␈αthe␈αfirst␈αargument␈αto␈αa
␈↓sum␈α
or␈α
product␈α
and␈α
used␈α
␈↓αcaddr␈↓␈α
to␈α
get␈α
the␈α
second.␈α
 We␈α
used␈α
␈↓αcar␈↓␈α
to␈α
extract␈α
the␈α
operator.

␈↓Let's␈α
define:
␈↓α␈↓ ¬pop[x] <= car[x]
␈↓α␈↓ ¬[arg␈↓β1␈↓α[x] <= cadr[x]
␈↓α␈↓ ¬Sarg␈↓β2␈↓α[x] <= caddr[x]

␈↓Then␈α
␈↓αdiff␈↓␈α
becomes:
␈↓␈↓↓30  Applications and Examples␈↓ 43.5␈↓



␈↓αdiff[u;x] <=
␈↓α␈↓ αX[atom [u] → [eq [x,u] → 1; T → 0];
␈↓α␈↓ αX eq [op[u]; PLUS] → list␈↓ ¬h[PLUS;
␈↓α␈↓ αX␈↓ ¬h diff [arg␈↓β1␈↓α [u]; x];
␈↓α␈↓ αX␈↓ ¬h diff [arg␈↓β2␈↓α [u]; x]]
␈↓α␈↓ αX eq [op[u]; TIMES] → list␈↓ ¬h[PLUS;
␈↓α␈↓ αX␈↓ ¬h list␈↓ εH[TIMES;
␈↓α␈↓ αX␈↓ ¬h␈↓ εH arg␈↓β1␈↓α [u];
␈↓α␈↓ αX␈↓ ¬h␈↓ εH diff [arg␈↓β2␈↓α [u]; x]];
␈↓α␈↓ αX␈↓ ¬h list␈↓ εH[TIMES;
␈↓α␈↓ αX␈↓ ¬h␈↓ εH arg␈↓β2␈↓α [u];
␈↓α␈↓ αX␈↓ ¬h␈↓ εH diff [arg␈↓β1␈↓α [u]; x]];
␈↓α␈↓ αX T → LOSER]

␈↓Still,␈αthere␈αis␈αmuch␈αof␈αthe␈αrepresentation␈α
present.␈αRecognition␈αof␈αvariables␈αand␈αother␈αterms␈α
can␈αbe
␈↓abstracted␈α∞from␈α∞the␈α∂representation.␈α∞We␈α∞need␈α∂only␈α∞recognize␈α∞when␈α∂a␈α∞term␈α∞is␈α∂a␈α∞sum,␈α∞a␈α∂product,␈α∞a
␈↓variable␈αor␈αa␈αconstant.␈α In␈αterms␈αof␈αthe␈αcurrent␈αrepresentation␈αwe␈αcould␈αdefine␈αsuch␈αrecognizers␈αor
␈↓predicates␈α
as:

␈↓α␈↓ ¬≠issum[x] <= eq[op[x];PLUS]
␈↓α␈↓ ¬∞isprod[x] <= eq[op[x];TIMES]
␈↓α␈↓ ¬2isconst[x] <= numberp[x]
␈↓α␈↓ β8isvar[x] <= [atom[x] → [isconst[x] → NIL; T → NIL]; T → NIL]
␈↓α␈↓ ¬=samevar[x;y] <= eq[x;y]


␈↓Using␈α
these␈α
predicates␈α
we␈α
can␈α
rewrite␈α
␈↓αdiff␈↓␈α
as:

␈↓αdiff[u;x] <=
␈↓α␈↓ αX[isvar[u] → [samevar[x,u] → 1; T → 0];
␈↓α␈↓ αX isconst[u] → 0;
␈↓α␈↓ αX issum[u] → list␈↓ ∧h[PLUS;
␈↓α␈↓ αX␈↓ ∧h diff [arg␈↓β1␈↓α [u]; x];
␈↓α␈↓ αX␈↓ ∧h diff [arg␈↓β2␈↓α [u]; x]]
␈↓α␈↓ αX isprod[u] → list␈↓ ∧h[PLUS;
␈↓α␈↓ αX␈↓ ∧h list␈↓ ¬H[TIMES;
␈↓α␈↓ αX␈↓ ∧h␈↓ ¬H arg␈↓β1␈↓α [u];
␈↓α␈↓ αX␈↓ ∧h␈↓ ¬H diff [arg␈↓β2␈↓α [u]; x]];
␈↓α␈↓ αX␈↓ ∧h list␈↓ ¬H[TIMES;
␈↓α␈↓ αX␈↓ ∧h␈↓ ¬H arg␈↓β2␈↓α [u];
␈↓α␈↓ αX␈↓ ∧h␈↓ ¬H diff [arg␈↓β1␈↓α [u]; x]];
␈↓α␈↓ αX T → LOSER]

␈↓Readability␈α
is␈α
certainly␈α
improving,␈α
but␈α
the␈α
representation␈αis␈α
still␈α
known␈α
to␈α
␈↓αdiff␈↓.␈α
 When␈α
we␈αbuild␈α
the
␈↓␈↓↓3.5␈↓ 	9Differentiation     31␈↓


␈↓result␈α⊂of␈α∂the␈α⊂sum␈α∂or␈α⊂product␈α∂of␈α⊂derivatives␈α∂we␈α⊂use␈α∂knowledge␈α⊂of␈α∂the␈α⊂representation.␈α⊂ Rather␈α∂it
␈↓would␈α
be␈α
better␈α
to␈α
define:

␈↓α␈↓ ¬↓makesum[x;y] <= list[PLUS;x;y]
␈↓α␈↓ ∧smakeprod[x;y] <= list[TIMES;x;y]

␈↓Then␈α
the␈α
new␈α
␈↓αdiff␈↓␈α
is:

␈↓αdiff[u;x] <=
␈↓α␈↓ αX[isvar[u] → [samevar[x,u] → 1; T → 0];
␈↓α␈↓ αX isconst[u] → 0;
␈↓α␈↓ αX issum[u] → makesum[diff [arg␈↓β1␈↓α [u]; x];diff[arg␈↓β2␈↓α [u]; x]]
␈↓α␈↓ αX isprod[u] → makesum[␈↓ ¬(makeprod[arg␈↓β1␈↓α [u];diff[arg␈↓β2␈↓α [u]; x]];
␈↓α␈↓ αX␈↓ ¬( makeprod[arg␈↓β2␈↓α [u];diff [arg␈↓β1␈↓α [u]; x]];
␈↓α␈↓ αX T → LOSER]

␈↓Notice␈αthat␈α
␈↓αdiff␈↓␈αis␈α
much␈αmore␈α
readable␈αnow␈α
and␈αmore␈α
importantly,␈αthe␈α
details␈αof␈αthe␈α
representation
␈↓have␈α
been␈α
relegated␈α
to␈α
subfunctions.␈α
Changing␈α
representation␈α
simply␈α
requires␈α∞supplying␈α
different
␈↓subfunctions.␈αNo␈α
changes␈αneed␈α
be␈αmade␈α
to␈α␈↓αdiff␈↓.␈α
There␈αhas␈α
only␈αbeen␈α
a␈αslight␈α
decrease␈αin␈α
efficiency.
␈↓The␈α⊃termination␈α⊃condition␈α⊂in␈α⊃the␈α⊃original␈α⊂␈↓αdiff␈↓␈α⊃is␈α⊃a␈α⊂bit␈α⊃more␈α⊃succinct.␈α⊂ Looking␈α⊃back,␈α⊃we␈α⊂first
␈↓abstracted␈α↔the␈α↔selector␈α↔functions,␈α↔those␈α↔which␈α↔selected␈α↔components;␈α↔then␈α↔we␈α↔abstracted␈α⊗the
␈↓recognizers,␈α∞the␈α
predicates␈α∞telling␈α
which␈α∞term␈α
was␈α∞present;␈α
then␈α∞we␈α
modified␈α∞the␈α∞constructors,␈α
the
␈↓functions␈α
which␈αmake␈α
new␈αterms.␈α
 These␈αthree␈α
components␈αof␈α
programming:␈α
selectors,␈αrecognizers,
␈↓and␈α
constructors␈α
will␈α
appear␈α
again␈α
on␈α
page␈α
133␈α
in␈α
discussing␈α
McCarthy's␈α
abstract␈α
syntax.



␈↓␈↓ ¬∃␈↓↓3.6  Algebra of polynomials␈↓


␈↓Assume␈α⊂we␈α⊂want␈α⊂to␈α⊃perform␈α⊂addition␈α⊂and␈α⊂multiplication␈α⊃of␈α⊂polynomials␈α⊂and␈α⊂assume␈α⊃that␈α⊂each
␈↓polynomial␈αis␈αof␈αthe␈αform␈α␈↓αp␈↓β1␈↓α␈α+␈αp␈↓β2␈↓α␈α+␈α...␈α+␈αp␈↓βn␈↓␈αwhere␈αeach␈α␈↓αp␈↓βi␈↓␈αis␈αa␈αproduct␈αof␈αvariables␈αand␈αconstants.
␈↓The␈αstandard␈αalgorithm␈αfor␈αaddition␈αof␈α␈↓λS␈↓πn␈↓βi=1␈↓αp␈↓βi␈↓␈αwith␈α␈↓λS␈↓πm␈↓βj=1␈↓αq␈↓βj␈↓␈αsays␈αyou␈αcan␈αcombine␈αa␈α␈↓αq␈↓βj␈↓␈αwith␈αa␈α
␈↓αp␈↓βi␈↓␈αif
␈↓the␈α
variable␈α
parts␈α
of␈αthese␈α
terms␈α
are␈α
identical.␈α In␈α
this␈α
case␈α
the␈αresulting␈α
term␈α
has␈α
the␈αsame␈α
variable
␈↓part␈αbut␈αhas␈αa␈αconstant␈αpart␈αequal␈αto␈αthe␈αsum␈αof␈αthe␈αconstant␈αparts␈αof␈α␈↓αp␈↓βi␈↓␈αand␈α␈↓αq␈↓βj␈↓.␈α For␈αexample␈αif␈α␈↓αp␈↓βi␈↓
␈↓is␈α
␈↓α2x␈↓␈α
and␈α
␈↓αq␈↓βj␈↓␈α
is␈α
␈↓α3x␈↓␈α
the␈α
sum␈α
term␈α
is␈α
␈↓α5x␈↓.

␈↓␈↓ ¬OFirst representation: 

␈↓We␈α∞could␈α
use␈α∞the␈α∞representation␈α
of␈α∞the␈α
differentiation␈α∞example.␈α∞ But␈α
since␈α∞we␈α
know␈α∞the␈α∞form␈α
of
␈↓polynomials␈α
this␈α
representation␈α
is␈α
more␈α
complex␈α
than␈α
necessary.

␈↓␈↓ ¬?Second representation: 

␈↓Using␈α	our␈α	knowledge␈α	of␈α	the␈α	forms␈α	of␈α	polynomials,␈α	write␈α	␈↓λS␈α
␈↓αp␈↓βi␈↓␈α	as:
␈↓␈↓↓32  Applications and Examples␈↓ 53.6␈↓


␈↓␈↓ ¬≥␈↓α( (␈↓rep of ␈↓αp␈↓β1␈↓α), (␈↓rep of ␈↓αp␈↓β2␈↓α) ...)␈↓ 

␈↓and␈α∪each␈α∪␈↓αp␈↓βi␈↓␈α∪is␈α∪represented␈α∪as␈α∪some␈α∩list␈α∪of␈α∪its␈α∪components␈α∪(constants␈α∪and␈α∪variables).␈α∪ Is␈α∩this
␈↓representation␈α
sufficient?␈α Does␈α
it␈αhave␈α
the␈αflexibility␈α
we␈αneed?␈α
 How␈αdo␈α
we␈αrepresent␈α
the␈α
test␈αfor
␈↓equal␈α⊃variable␈α⊂parts␈α⊃of␈α⊂␈↓αp␈↓βi␈↓α␈α⊃and␈α⊃q␈↓βj␈↓?␈α⊂ ␈↓αequal␈↓␈α⊃does␈α⊂it.␈α⊃ If␈α⊂they␈α⊃are␈α⊃equal␈α⊂then␈α⊃we␈α⊂build␈α⊃a␈α⊃new␈α⊂list
␈↓representing␈α
the␈α
same␈α
variable␈α
part␈α
but␈α
with␈α
a␈α
constant␈α
part␈α
equal␈α
to␈α
the␈α
sum.

␈↓␈↓ ¬EThird representation: 

␈↓␈↓αequal␈↓␈αis␈αexpensive.␈α Can␈αwe␈αdo␈αbetter?␈α That␈αis␈αcan␈αwe␈αrepresent␈αsome␈αterm␈αlike␈α␈↓α2*x␈↓π2␈↓α␈α*y␈↓π3␈↓α*␈αz␈↓␈αbetter
␈↓than␈α
say

␈↓␈↓ ∧)␈↓α(2, (EXPT, X, 2) (EXPT Y 3) (EXPT Z,1))␈↓? 

␈↓First,␈α
␈↓αEXPT␈↓␈α
is␈α
really␈α
unnecessary;␈α
the␈α
list:

␈↓␈↓ βz␈↓α(2, (X 2), (Y 3), (Z 1))␈↓, carries the same information. 

␈↓Now␈αif␈αwe␈αassume␈αthat␈αwe␈αknow␈αhow␈αmany␈αvariables␈αcan␈αever␈αappear␈αand␈αthen␈αassume␈αthat␈αthose
␈↓variables␈α
always␈α
appear␈α
in␈α
the␈α
same␈α
order␈α
in␈α
a␈α
term␈α
then␈α
we␈α
could␈α
write␈α
the␈α
above␈α
as:

␈↓␈↓ ∧∧␈↓α( 2, 2, 3, 1 )␈↓ assuming ␈↓αx, y, z␈↓ are the only variables. 

␈↓Let`s␈α
stop␈α
for␈α
some␈α
examples.

␈↓␈↓ ¬_␈↓↓term␈↓ πλrepresentation
␈↓↓␈↓ ¬_␈↓α2xyz␈↓ πλ(2, 1, 1, 1)
␈↓α␈↓ ¬_2x␈↓π2␈↓αz␈↓ πλ(2, 2, 0, 1)
␈↓α␈↓ ¬_4z␈↓π3␈↓α␈↓ πλ(4, 0, 0, 3)

␈↓Now␈α⊃let`s␈α⊃think␈α⊃about␈α⊃the␈α⊃structure␈α⊃of␈α⊃the␈α⊃main␈α⊃algorithm.␈α⊃ The␈α⊃algorithm␈α⊃for␈α⊃the␈α⊃sum␈α⊂must
␈↓compare␈α
terms␈α
finding␈α
like␈α
terms␈α
generated␈α
a␈α
new␈α
term,␈α
otherwise␈α
simply␈α
copy␈α
the␈α
terms.

␈↓␈↓ ¬@Fourth representation: 

␈↓When␈αwe␈αpick␈α
a␈α␈↓αp␈↓βi␈↓␈αfrom␈αthe␈α
first␈αpolynomial␈αwe␈α
would␈αlike␈αto␈αfind␈α
the␈αcorresponding␈α␈↓αq␈↓βj␈↓␈α
with␈αthe
␈↓minimum␈α∩amount␈α⊃of␈α∩searching.␈α⊃ This␈α∩can␈α⊃be␈α∩accomplished␈α⊃if␈α∩we␈α⊃can␈α∩order␈α⊃the␈α∩terms␈α∩in␈α⊃the
␈↓polynomials.␈αSo␈αwe␈αwill␈αassume␈αthat␈αthere␈αis␈αa␈αmaximum␈αnumber␈αof␈αdigits␈αin␈αthe␈αexponents␈αof␈αany
␈↓term.␈α
 For␈α
sake␈α
of␈α
argument,␈α
assume␈α
2␈α
digits␈α
then␈α
we␈α
will␈α
represent␈α
terms␈α
as␈α
follows:

␈↓␈↓ ¬_␈↓↓terms␈↓ πλrepresentation

␈↓↓␈↓ ¬_␈↓α2x*y*z␈↓ πλ(2, 010101)
␈↓α␈↓ ¬_2x␈↓π2␈↓αz␈↓ πλ(2, 020001)
␈↓α␈↓ ¬_4z␈↓π3␈↓α␈↓ πλ(4, 000003)
␈↓␈↓↓3.6␈↓ λIAlgebra of polynomials     33␈↓


␈↓Now␈α∂we␈α⊂can␈α∂order␈α∂on␈α⊂the␈α∂numeric␈α⊂representation␈α∂of␈α∂the␈α⊂variable␈α∂part␈α∂of␈α⊂the␈α∂term.␈α⊂ One␈α∂more
␈↓simplification:

␈↓␈↓ ∧vrepresent ␈↓α ax␈↓πA␈↓α*y␈↓πB␈↓α*z␈↓πC␈↓ as ␈↓α(a . ABC). 

␈↓α␈↓Finally␈αwe␈αwill␈αwrite␈αthe␈αalgorithm.␈α We␈αwill␈αassume␈αthat␈αthe␈αpolynomials␈αare␈αinitially␈αordered␈αand
␈↓will␈α
write␈α
the␈α
algorithm␈α
so␈α
as␈α
to␈α
maintain␈αthat␈α
ordering.␈α
 Each␈α
term␈α
is␈α
a␈α
dotted␈α
pair␈α
of␈αelements:␈α
the
␈↓coefficient␈α
and␈α
a␈α
representation␈α
of␈α
the␈α
exponent.

␈↓As␈α~in␈α~the␈α~previous␈α~differentiation␈α~example,␈α~we␈α~should␈α~extract␈α~the␈α~algorithm␈α≠from␈α~the
␈↓representation.␈α
 We␈α
shall␈α
define:

␈↓␈↓ ∧F␈↓αcoef[x] <= car[x]␈↓ and ␈↓α expo[x] <= cdr[x].  ␈↓ 

␈↓To␈α
test␈α
the␈α
ordering␈α
we␈α
will␈α
use␈α
the␈α
LISP␈α
predicate:

␈↓␈↓ ¬H␈↓αgreaterp[x;y] = x>y.  ␈↓ 

␈↓In␈α
the␈α
construction␈α
of␈α
the␈α
`sum'␈α
polynomial␈α
we␈α
will␈α
generate␈α
new␈α
terms␈α
by␈α
combining␈αcoefficients.
␈↓So␈α∂a␈α∞function␈α∂is␈α∞needed␈α∂to␈α∞reflect␈α∂this␈α∂construction.␈α∞␈↓αnode[x;y]␈↓␈α∂where␈α∞␈↓αx␈↓␈α∂is␈α∞a␈α∂representation␈α∂of␈α∞the
␈↓coefficient␈α
and␈α
␈↓αy␈↓␈α
is␈α
a␈α
representation␈α
of␈α
the␈α
exponent␈α
is␈α
thus␈α
defined␈α
as:

␈↓␈↓ ¬C␈↓αnode[x;y] <= cons[x;y].␈↓ 

␈↓So␈α
here's␈α
a␈α
representation␈α
of␈α
a␈α
polynomial:

␈↓␈↓ ∧C␈↓αx␈↓π2␈↓α - 2y - z ␈↓(assuming max exponent of 9.) 



␈↓α                                        ␈↓note:␈↓α 200>010>001
␈↓α  1   200


␈↓α         -2    010


␈↓α                  -1    001       NIL
␈↓␈↓↓34  Applications and Examples␈↓ 53.6␈↓


␈↓Here's the algorithm:
␈↓αpolyadd[p;q] <=
␈↓α␈↓ ↓h[null[p] →q; null[q] → p;
␈↓α␈↓ ↓h eq[expo[car[p]];expo[car[q]]] →␈↓ ¬h[zerop[plus[coef[car[p]];coef[car[q]]]]
␈↓α␈↓ ↓h␈↓ αX␈↓ ¬h    → polyadd[cdr[p];cdr[q]]];
␈↓α␈↓ ↓h␈↓ αX␈↓ ¬h T → cons[node[plus[coef[car[p]];coef[car[q]]];
␈↓α␈↓ ↓h␈↓ αX␈↓ ¬h             expo[car[p]]];polyadd[cdr[p];cdr[q]]]]
␈↓α␈↓ ↓h greaterp[expo[car[p]];expo[car[q]]] → cons[car[p];polyadd[cdr[p];q]];
␈↓α␈↓ ↓h T → cons[car[q];polyadd[p;cdr[q]]]]

␈↓Now for an explanation and example:

␈↓First we used some new LISP functions:

␈↓␈↓ ¬f␈↓αplus[x;y] = x + y
␈↓α␈↓ ¬Rzerop[x]  <= eq[x;0]

␈↓␈↓αpolyadd␈↓ is of the form: ␈↓α[p␈↓β1␈↓α → e␈↓β1␈↓α; p␈↓β2␈↓α → e␈↓β2␈↓α; p␈↓β3␈↓α → e␈↓β3␈↓α; p␈↓β4␈↓α → e␈↓β4␈↓α; p␈↓β5␈↓α → e␈↓β5␈↓α]


␈↓Case␈α
i:␈α
␈↓αp␈↓β1␈↓α␈α
→␈α
e␈↓β1␈↓␈α
and␈α
␈↓αp␈↓β2␈↓α␈α
→␈α
e␈↓β2␈↓.␈α
If␈α
either␈α
polynomial␈α
is␈α
empty␈α
return␈α
the␈α
other.

␈↓Case␈αii:␈α␈↓αp␈↓β3␈↓α␈α→␈αe␈↓β3␈↓.␈α If␈αthe␈αexponents␈αare␈αequal␈αthen␈αwe␈αcan␈αthink␈αabout␈αcombining␈αterms.␈α However,
␈↓␈↓ αhwe␈α∩must␈α∩check␈α∩for␈α∩cancellations␈α∩and␈α∩not␈α∩include␈α∩any␈α∩such␈α∩terms␈α∩in␈α∩our␈α∩resultant
␈↓␈↓ αhpolynomial.

␈↓Case␈α⊂iii:␈α⊂␈↓αp␈↓β4␈↓α␈α⊂→␈α⊂e␈↓β4␈↓␈α⊂and␈α⊂␈↓αp␈↓β5␈↓α␈α⊂→␈α⊂e␈↓β5␈↓.␈α∂Theses␈α⊂sections␈α⊂worry␈α⊂about␈α⊂the␈α⊂ordering␈α⊂of␈α⊂terms␈α⊂so␈α⊂that␈α∂the
␈↓␈↓ αhresultant␈α
polynomial␈α
retains␈α
the␈α
ordering.

␈↓Here's an informal execution of ␈↓αpolyadd:

␈↓αpolyadd[x+y+z;x␈↓π2␈↓α-2y-z]
␈↓α␈↓ αX= cons[x␈↓π2␈↓α;polyadd[x+y+z;-2y-z]]
␈↓α␈↓ αX= cons[x␈↓π2␈↓α;cons[x;polyadd[y+z;-2y-z]]]
␈↓α␈↓ αX= cons[x␈↓π2␈↓α;cons[x;cons[node[1+-2;y];polyadd[z;-z]]]]
␈↓α␈↓ αX= cons[x␈↓π2␈↓α;cons[x;cons[-y;polyadd[z;-z]]]]
␈↓α␈↓ αX= cons[x␈↓π2␈↓α;cons[x;cons[-y;polyadd[NIL;NIL]]]]
␈↓α␈↓ αX= cons[x␈↓π2␈↓α;cons[x;cons[-y;NIL]]]
␈↓α␈↓ ε≠= x␈↓π2␈↓α+x-y
␈↓␈↓↓3.7␈↓ λ"Evaluation of polynomials     35␈↓


␈↓␈↓ ¬↓␈↓↓3.7  Evaluation of polynomials␈↓


␈↓Given␈α∞an␈α∞arbitrary␈α
polynomial,␈α∞and␈α∞values␈α
for␈α∞any␈α∞of␈α
the␈α∞variables␈α∞which␈α
it␈α∞contains,␈α∞we␈α
would
␈↓like␈α⊃to␈α⊃compute␈α⊃its␈α⊂value.␈α⊃For␈α⊃this␈α⊃section␈α⊂we␈α⊃will␈α⊃assume␈α⊃that␈α⊂the␈α⊃substitutions␈α⊃of␈α⊃values␈α⊂for
␈↓variables␈αhas␈αalready␈αbeen␈α
carried␈αout.␈αThus␈αwe␈αare␈α
dealing␈αwith␈αpolynomials␈αof␈αthe␈α
form:␈α␈↓λS␈↓πn␈↓βi=1␈↓αp␈↓βi␈↓
␈↓where␈α
␈↓αp␈↓βi␈↓␈α
is␈α
a␈α
product␈α
of␈α
powers␈α
of␈α
constants.␈α
For␈α
example:

␈↓␈↓ ∧4␈↓α2␈↓π3␈↓α + 3*4␈↓π2␈↓α + 5.␈↓ This could be represented as: 

␈↓␈↓ β←␈↓α(PLUS (EXPT 2 3)(PLUS (TIMES 3(EXPT 4 2)) 5)).␈↓ 

␈↓We␈α∞will␈α∞now␈α∞describe␈α∞a␈α∞LISP␈α∞function,␈α∂␈↓αvalue␈↓,␈α∞which␈α∞will␈α∞take␈α∞such␈α∞an␈α∞sexpr␈α∂representation␈α∞and
␈↓compute␈α⊃its␈α⊂value.␈α⊃First,␈α⊃input␈α⊂to␈α⊃␈↓αvalue␈↓␈α⊂will␈α⊃be␈α⊃numbers␈α⊂or␈α⊃lists␈α⊂beginning␈α⊃with␈α⊃either␈α⊂␈↓αPLUS,
␈↓αTIMES,␈↓␈αor␈α
␈↓αEXPT␈↓.␈α The␈αvalue␈α
of␈αa␈αnumber␈α
is␈αthat␈αnumber;␈α
to␈αevaluate␈αthe␈α
other␈αforms␈α
of␈αinput
␈↓we␈α∞should␈α
perform␈α∞the␈α
operation␈α∞represented.␈α
We␈α∞must␈α
therefore␈α∞assume␈α
that␈α∞operations␈α∞of␈α
plus,
␈↓times␈αand␈αexponentiation␈αexits.␈α Assume␈αthey␈αare␈αnamed␈α
+,␈α*,␈αand␈α↑,␈αresp.␈αWhat␈αthen␈αshould␈αbe␈α
the
␈↓value␈αof␈αa␈αlist␈αwhose␈α␈↓αcar␈↓-part␈αis␈α␈↓αPLUS␈↓?␈αIt␈αshould␈αbe␈αthe␈αresult␈αof␈αadding␈αthe␈αvalue␈αof␈αthe␈α␈↓αcadr␈↓␈αof
␈↓the␈α∂list␈α∂to␈α∂the␈α∂value␈α∞of␈α∂the␈α∂␈↓αcaddr␈↓␈α∂of␈α∂the␈α∞list.␈α∂That␈α∂is␈α∂␈↓αvalue␈↓␈α∂is␈α∞clearly␈α∂recursive.␈α∂ To␈α∂test␈α∂for␈α∞the
␈↓occurrence␈αof␈α
a␈αnumber␈α
we␈αshall␈α
assume␈αa␈α
unary␈αLISP␈α
predicate␈αcalled␈α
␈↓αnumberp␈↓␈αwhich␈α
returns␈α␈↓αT␈↓
␈↓just␈α
in␈α
the␈α
case␈α
that␈α
its␈α
argument␈α
is␈α
a␈α
number.

␈↓It␈α
should␈α
now␈α
be␈α
clear␈α
how␈α
to␈α
write␈α
␈↓αvalue␈↓:
␈↓αvalue[x] <=␈↓ αX[numberp[x] → x;
␈↓α␈↓ αX eq[car[x] PLUS] → +[value[cadr[x]];value[caddr[x]]];
␈↓α␈↓ αX eq[car[x] TIMES] → *[value[cadr[x]];value[caddr[x]]];
␈↓α␈↓ αX eq[car[x] EXPT] → ↑[value[cadr[x]];value[caddr[x]]]]


␈↓Or␈α
more␈α
abstractly:
␈↓αvalue[x] <=␈↓ αX[isnumber[x] → x;
␈↓α␈↓ αX issum[x] → +[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]];
␈↓α␈↓ αX isprod[x] → *[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]];
␈↓α␈↓ αX isexpt[x] → ↑[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]]]




␈↓␈↓ ¬u␈↓↓3.8  Problems␈↓


␈↓␈↓↓I.␈↓␈α
This␈α
problem␈α
deals␈α
with␈α
the␈α
function␈α
␈↓αvalue␈↓␈α
from␈α
Section␈α
3.7.

␈↓1.␈α
 Show␈α
how␈α
to␈α
extend␈α
␈↓α␈α
value␈↓␈α
to␈α
handle␈α
binary␈α
and␈α
unary␈α
minus.
␈↓␈↓↓36  Applications and Examples␈↓ 43.8␈↓


␈↓2.␈α Write␈αa␈αfunction,␈α␈↓αinstantiate␈↓␈αwhich␈αwill␈αtake␈αtwo␈αarguments,␈αone␈αrepresenting␈αa␈αset␈αof␈αvariables
␈↓and␈α∞values;␈α∞the␈α∞other␈α∂representing␈α∞a␈α∞polynomial.␈α∞ ␈↓αinstantiate␈↓␈α∞is␈α∂to␈α∞return␈α∞a␈α∞representation␈α∂of␈α∞the
␈↓polynomial␈α
which␈α
would␈α
result␈α
from␈α
substituting␈α
the␈α
values␈α
for␈α
the␈α
variables.

␈↓3.␈α⊂ It␈α⊂would␈α∂be␈α⊂nice␈α⊂if␈α∂we␈α⊂could␈α⊂represent␈α∂expressions␈α⊂like␈α⊂2+3+4␈α∂as␈α⊂␈↓α(PLUS 2 3 4)␈↓␈α⊂rather␈α∂than
␈↓␈↓α(PLUS (PLUS 2 3) 4)␈↓␈α?␈α↓or␈α?␈α↓␈↓α(PLUS 2(PLUS 3 4))␈↓;␈α?or␈α?␈α↓represent␈α?␈α↓2*3*4+5+6␈α?as
␈↓␈↓α(PLUS (TIMES 2 3 4) 5 6)␈↓.

␈↓Write␈α
a␈α
new␈α
version␈α
of␈α
␈↓αvalue␈↓␈α
which␈α
can␈α
evaluate␈α
such␈α
n-ary␈α
representations␈α
of␈α
+␈α
and␈α
*.
␈↓␈↓↓3.9␈↓ 	⊃The great mothers     37␈↓


␈↓␈↓ ¬9␈↓↓3.9  The great mothers␈↓


␈↓␈↓ ∧∪␈↓↓I. The Great Mother of All Functions!!! (␈↓αtgmoaf␈↓↓)

␈↓α␈↓ αXtgmoaf[x] <=␈↓ ∧H[atom[x] →␈↓ ¬x[eq[x ;T] → T;
␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x eq[x;NIL] → NIL;
␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x T → TRYAGAINNEXTWEEK];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];QUOTE] → cadr[x];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];CAR] → car[tgmoaf[cadr[x]]];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];CDR] → cdr[tgmoaf[cadr[x]]];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];CONS] → cons[tgmoaf[cadr[x]];tgmoaf[caddr[x]]];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];ATOM] → atom[tgmoaf[cadr[x]]];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];EQ] → eq[tgmoaf[cadr[x]];tgmoaf[caddr[x]]];
␈↓α␈↓ αX␈↓ ∧H T → TRYAGAINNEXTWEEK]

␈↓Evaluate the following:

␈↓␈↓↓1.␈↓α tgmoaf[T]
␈↓α␈↓↓2.␈↓α tgmoaf[A]
␈↓α␈↓↓3.␈↓α tgmoaf[(CAR(QUOTE(A . B)))]
␈↓α␈↓↓4.␈↓α tgmoaf[(CDR (QUOTE (A B)))]
␈↓α␈↓↓5.␈↓α tgmoaf[(EQ (CAR (QUOTE (A . B)))(QUOTE A))]
␈↓α␈↓↓6.␈↓α tgmoaf[(EQ (CAR (QUOTE (A . B))) A)]
␈↓α␈↓↓7.␈↓α tgmoaf[(ATOM (CAR (QUOTE (A B))))]



␈↓α␈↓ βI␈↓↓II. The Great Mother of All Functions Revisited!!!(␈↓αtgmoafr␈↓↓)

␈↓α␈↓ αXtgmoafr[x] <=␈↓ ∧H[atom[x] →␈↓ ¬x[eq[x;T] → T;
␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x eq[x;NIL] → NIL;
␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x T → TRYAGAINNEXTWEEK];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];QUOTE] → cadr[x];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];CAR] → car[tgmoafr[cadr[x]]];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];CDR] → cdr[tgmoafr[cadr[x]]];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];CONS] → cons[tgmoafr[cadr[x]];tgmoafr[caddr[x]]];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];ATOM] → atom[tgmoafr[cadr[x]]];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];EQ] → eq[tgmoafr[cadr[x]];tgmoafr[caddr[x]]];
␈↓α␈↓ αX␈↓ ∧H eq[car[x];COND] → evcond[cdr[x]];
␈↓α␈↓ αX␈↓ ∧H T → TRYAGAINNEXTWEEK]

␈↓α␈↓ αXevcond[x] <=␈↓ ∧H[tgmoafr[caar[x]] → tgmoafr[cadar[x]];
␈↓α␈↓ αX␈↓ ∧H T → evcond[cdr[x]] ]
␈↓␈↓↓38  Applications and Examples␈↓ 53.9␈↓



␈↓Evaluate the following:

␈↓␈↓↓1.␈↓α tgmoafr[T]
␈↓α␈↓↓2.␈↓α tgmoafr[(CDR (QUOTE (A B)))]
␈↓α␈↓↓3.␈↓α tgmoafr[(EQ (CAR (QUOTE (A . B))) (QUOTE A))]
␈↓α␈↓↓4.␈↓α tgmoafr[(COND((EQ (CAR (QUOTE (A . B)))(QUOTE A))(QUOTE FOO)))]
␈↓α␈↓↓5.␈↓α tgmoafr[(COND((ATOM (QUOTE (A)))(QUOTE FOO))(T(QUOTE BAZ)))]


␈↓α␈↓ ∧cComing soon: ␈↓↓Son of Great Mother !!␈↓α
␈↓␈↓↓3.10␈↓ π\Proving properties of programs     39␈↓


␈↓␈↓ ∧X␈↓↓3.10  Proving properties of programs␈↓


␈↓People␈α
are␈α
becoming␈αincreasingly␈α
aware␈α
of␈αthe␈α
importance␈α
of␈αgiving␈α
convincing␈α
arguments␈αfor␈α
such
␈↓things␈αas␈αthe␈αcorrectness␈αor␈αequivalence␈αof␈αprograms.␈αThese␈αare␈αboth␈αvery␈αdifficult␈αenterprises.␈αWe
␈↓will␈αonly␈αsketch␈αa␈α
proof␈αof␈αa␈αsimple␈αproperty␈α
of␈αtwo␈αprograms␈αand␈α
leave␈αothers␈αas␈αproblems␈αfor␈α
the
␈↓interested␈α
reader.

␈↓Using␈α
the␈αdefinition␈α
of␈α␈↓αappend␈↓␈α
given␈αon␈α
page␈α22␈α
and␈αthe␈α
definition␈αof␈α
␈↓αreverse␈↓␈αgiven␈α
on␈α
page␈α22,
␈↓we␈α
wish␈α
to␈α
show␈α
that:

␈↓␈↓ ∧α␈↓αappend[reverse[y];reverse[x]] = reverse[append[x;y]]␈↓, 

␈↓for␈α
any␈α
lists,␈α
␈↓αx␈↓,␈α
and␈α
␈↓αy␈↓.␈α
The␈α
induction␈α
will␈α
be␈α
on␈α
the␈α
structure␈α
of␈α
␈↓αx␈↓.
␈↓␈↓ αλ|␈↓↓Basis␈↓: ␈↓αx␈↓ is ␈↓αNIL␈↓.
␈↓␈↓ αλ|We must thus show: ␈↓αappend[reverse[y];NIL] = reverse[append[NIL;y]]␈↓
␈↓␈↓ αλ|But: ␈↓αreverse[append[NIL;y]] = reverse[y]␈↓  by the def. of ␈↓αappend␈↓.
␈↓␈↓ αλ|We now establish the stronger result:  ␈↓αappend[z;NIL] = z␈↓
␈↓␈↓ αλ␈↓ αX|␈↓↓Basis:␈↓ ␈↓αz␈↓ is ␈↓αNIL␈↓.
␈↓␈↓ αλ␈↓ αX|Show ␈↓αappend[NIL;NIL] = NIL␈↓. Easy.
␈↓␈↓ αλ␈↓ αX|␈↓↓Induction step:␈↓ Assume the lemma for lists, ␈↓αz␈↓, of length n;
␈↓␈↓ αλ␈↓ αX|Prove: ␈↓αappend[cons[x;z];NIL] = cons[x;z]␈↓.
␈↓␈↓ αλ␈↓ αX|Since ␈↓αcons[...]␈↓ is not ␈↓αNIL␈↓, then applying the definition of ␈↓αappend␈↓
␈↓␈↓ αλ␈↓ αX|says we must prove: ␈↓αcons[x;append[z;NIL]] = cons[x;z]␈↓.
␈↓␈↓ αλ␈↓ αX|But an application of the induction hypothesis gives our result.
␈↓␈↓ αλ|So the Basis for our main result is established.
␈↓␈↓ αλ|␈↓↓Induction step:␈↓ Assume the result for lists, ␈↓αz␈↓, of length n;
␈↓␈↓ αλ|Prove:
␈↓␈↓ αλ|␈↓ αU   (1)   ␈↓αappend[reverse[y];reverse[cons[x;z]]] = reverse[append[cons[x;z];y]␈↓
␈↓␈↓ αλ| Using the definition of ␈↓αreverse␈↓ on the LHS of (1) gives:
␈↓␈↓ αλ|␈↓ β]   (2)   ␈↓αappend[reverse[y];append[reverse[z];list[x]]]␈↓.
␈↓␈↓ αλ| Using the definition of ␈↓αappend␈↓ on the RHS of (1) gives:
␈↓␈↓ αλ|␈↓ ∧K   (3)   ␈↓αreverse[cons[x;append[z;y]].␈↓
␈↓␈↓ αλ| Using the definition of ␈↓αreverse␈↓ on (3) gives:
␈↓␈↓ αλ|␈↓ ∧∩   (4)   ␈↓αappend[reverse[append[z;y];list[x]]].␈↓
␈↓␈↓ αλ| Using our induction hypothesis on (4) gives:
␈↓␈↓ αλ|␈↓ β`   (5)   ␈↓αappend[append[reverse[y];reverse[z]];list[x]]␈↓
␈↓␈↓ αλ| Thus we must establish that    (2) = (5).
␈↓␈↓ αλ| But this is just an instance of the associativity of ␈↓αappend␈↓:
␈↓␈↓ αλ|␈↓ αR␈↓αappend[x;append[y;z]] = append[append[x;y];z].␈↓  (See problem I, below.)

␈↓The␈αstructure␈αof␈αthe␈αproof␈αis␈αanalogous␈αto␈αproofs␈αby␈αmathematical␈αinduction␈αin␈αelementary␈α
number
␈↓theory.␈αThe␈αability␈αto␈αperform␈αsuch␈αproofs␈αis␈αa␈αdirect␈αconsequence␈αof␈αour␈αcareful␈αdefinition␈αof␈αdata
␈↓structures.␈α Examination␈αof␈αthe␈αproof␈αwill␈αshow␈αthat␈αthere␈αis␈αa␈αclose␈αrelationship␈αbetween␈αwhat␈αwe
␈↓are␈α∪inducting␈α∪on␈α∪in␈α∪the␈α∩proof␈α∪and␈α∪what␈α∪we␈α∪are␈α∩recurring␈α∪on␈α∪during␈α∪the␈α∪evaluation␈α∪of␈α∩the
␈↓␈↓↓40  Applications and Examples␈↓ )3.10␈↓


␈↓expressions.␈αA␈αprogram␈αwritten␈αby␈αBoyer␈αand␈αMoore␈αhas␈αbeen␈αreasonably␈αsuccessful␈α
in␈αgenerating
␈↓proofs␈α
like␈α
the␈α
above␈α
by␈α
exploiting␈α
this␈α
relationship.

␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓I.␈α
Prove␈α
the␈α
associativity␈α
of␈α
␈↓αappend␈↓.

␈↓II␈α
Analysis␈αof␈α
the␈α
above␈αproof␈α
shows␈α
frequent␈αuse␈α
of␈α
other␈αresults␈α
for␈α
LISP␈αfunctions.␈α
Fill␈α
in␈αthe
␈↓details.␈α
Investigate␈α
the␈α
possibility␈α
of␈α
formalizing␈α
this␈α
proof,␈α
showing␈α
what␈α
axioms␈α
are␈α
needed.

␈↓III␈α
Show␈α
the␈α
equivalence␈α
of␈α
␈↓αfact␈↓␈α
(page␈α
18)␈α
and␈α
␈↓αfact␈↓β1␈↓␈α
(page␈α
20).

␈↓IV␈α
Show␈α
the␈α
equivalence␈α
of␈α
␈↓αlength␈↓␈α
and␈α
␈↓αlength␈↓β1␈↓␈α
(page␈α
21).

␈↓V␈α
Using␈α
the␈α
definition␈α
of␈α
␈↓αreverse␈↓,␈α
given␈α
on␈α
page␈α
21,␈α
prove:

␈↓␈↓ ¬A␈↓αreverse[reverse[x]] = x␈↓. 

␈↓VI␈αProve␈αthat␈αthe␈αconstruction␈α
␈↓αatom[x]␈α→␈αeq[x;y]␈↓␈αis␈αequivalent␈α
to␈α␈↓αatom[x] → [eq[x;y] →T; T → NIL]␈↓.
␈↓See␈α
page␈α
17.
␈↓␈↓↓4.␈↓ 	xEvaluation     41␈↓


␈↓␈↓ ¬⎇␈↓↓SECTION 4

␈↓↓␈↓ ∧6EVALUATION OF LISP EXPRESSIONS␈↓






␈↓␈↓ ¬←␈↓↓4.1  Introduction␈↓


␈↓We␈αshall␈αnow␈αbegin␈αto␈α
look␈αvery␈αclosely␈αat␈αthe␈αintuitive␈α
process␈αwhich␈αwe␈αhave␈αbeen␈αusing␈α
in␈αthe
␈↓evaluation␈α∃of␈α⊗LISP␈α∃expressions.␈α⊗ Why?␈α∃First␈α⊗in␈α∃an␈α∃abstract␈α⊗sense,␈α∃that␈α⊗is␈α∃the␈α⊗business␈α∃of
␈↓programming␈α
languages--evaluation␈αof␈α
functions.␈α
 They␈αaren't␈α
called␈α
procedure-oriented␈αlanguages
␈↓for␈α∞nothing!␈α∞ Also␈α
function␈α∞evaluation␈α∞is␈α
a␈α∞basic␈α∞tool␈α
of␈α∞evaluation␈α∞of␈α
LISP␈α∞functions.␈α∞ In␈α∞fact␈α
a
␈↓very␈α∩general␈α∩kind␈α⊃of␈α∩evaluation␈α∩process␈α⊃is␈α∩going␈α∩on␈α⊃in␈α∩LISP,␈α∩that␈α⊃is␈α∩evaluation␈α∩of␈α⊃recursive
␈↓functions.␈α
 Later␈α
we␈α
will␈α
study␈α
the␈α
mechanisms␈α
which␈α
will␈α
support␈α
recursive␈α
evaluation.

␈↓First,␈αas␈αwe␈αmentioned␈αin␈αregard␈αto␈αthe␈αpolynomial␈αarithmetic␈αexamples␈αthe␈αintuitive␈αmathematical
␈↓operations␈αsay␈α
nothing␈αabout␈α
the␈αprocess␈α
of␈αevaluation.␈α
 ␈↓α(2*3)␈α+␈α
(5*6)␈↓␈αevaluates␈α
to␈α␈↓α36␈↓␈αregardless␈α
of
␈↓whether␈αyou␈α
evaluate␈α␈↓α2*3␈↓␈α
first␈αor␈α␈↓α5*6␈↓␈α
first,␈αor␈α
whether␈αyou␈αdo␈α
them␈αin␈α
parallel.␈α Probably␈α
if␈αone
␈↓were␈α
forced␈α
to␈α
explicate␈α
mathematical␈α
evaluation,␈α
we␈α
would␈α
have␈α
to␈α
say␈α
parallel␈α
evaluation.

␈↓Already␈αon␈α
page␈α11␈α
we␈αhave␈α
seen␈αthat␈αorder␈α
of␈αevaluation␈α
in␈αconditional␈α
expressions␈αcan␈α
make␈αa
␈↓difference,␈α⊃and␈α⊃certainly␈α⊃when␈α⊃we␈α⊃try␈α⊃to␈α⊃mechanize␈α⊃LISP␈α⊃function␈α⊃evaluation␈α⊃on␈α∩a␈α⊃sequential
␈↓machine␈α∂we␈α⊂␈↓↓will␈↓␈α∂have␈α⊂to␈α∂choose␈α⊂␈↓↓some␈↓␈α∂order␈α⊂of␈α∂evaluation.␈α⊂ We␈α∂must␈α⊂also␈α∂make␈α⊂some␈α∂decision
␈↓regarding␈α
the␈α
order␈α
of␈α
evaluation␈α∞of␈α
the␈α
arguments␈α
to␈α
a␈α∞function␈α
call,␈α
say␈α
␈↓αf[x␈↓β1␈↓α;x␈↓β2␈↓α;␈α
...x␈↓βn␈↓].␈α∞ We␈α
will
␈↓assume␈α
that␈α
we␈α
will␈α
evaluate␈α
the␈α
arguments␈α
from␈α
left␈α
to␈α
right.␈α
 The␈α
evaluation␈α
scheme␈α∞which␈α
we
␈↓choose␈α⊂is␈α⊃called␈α⊂␈↓↓inside-out␈↓␈α⊂or␈α⊃␈↓↓call-by-value␈↓.␈α⊂ Alternative␈α⊂proposals␈α⊃exist␈α⊂and␈α⊂have␈α⊃their␈α⊂merits;
␈↓call-by-name␈α∀(outside-in)␈α∃evaluation␈α∀is␈α∀another␈α∃well-known␈α∀scheme.␈α∀ Here␈α∃again,␈α∀the␈α∃style␈α∀of
␈↓evaluation␈α
can␈α
make␈α
a␈α
difference.␈α
Consider␈α
the␈α
example␈α
due␈α
to␈α
J.␈α
Morris:

␈↓␈↓αf[x;y]␈α<=␈α[x␈α=␈α0␈α→␈α0;␈αT␈α→␈αf[x-1;f[y-2;x]]]]␈↓.␈αEvaluation␈αof␈α␈↓αf[2;1]␈↓␈αwill␈αterminate␈αif␈αwe␈αalways␈αevaluate
␈↓the␈α
leftmost-outermost␈α
occurrence␈α
of␈α
␈↓αf␈↓.␈α
 Thus:

␈↓␈↓ ∧9␈↓αf[2;1] = f[1;f[-1;2]] = f[0;f[f[-1;2]-2;-1] = 0;␈↓ 

␈↓However,␈α∃if␈α∃we␈α∃evaluate␈α∃the␈α∃rightmost-innermost␈α∃occurrences␈α∃first␈α∃the␈α∃computation␈α∃will␈α∃not
␈↓terminate.

␈↓Intuitively,␈αcall-by-value␈αsays:␈αevaluate␈αthe␈αarguments␈αto␈αa␈αfunction␈αbefore␈αyou␈αattempt␈αto␈αapply␈αthe
␈↓arguments␈α
to␈α
the␈α
function␈α
definition.␈α
 Here␈α
are␈α∞some␈α
examples:␈α
Let␈α
␈↓αf[x;y]␈↓␈α
be␈α
␈↓αx␈↓π2␈↓α␈α
+␈α
y␈↓␈α∞and␈α
consider
␈↓␈↓αf[3+4;␈α⊂2*2]␈↓.␈α⊂ Then␈α⊂call-by-value␈α⊂says␈α⊂evaluate␈α⊂the␈α⊂arguments␈α⊂(␈↓α7␈↓ and ␈↓α4␈↓)␈α⊂associate␈α⊂those␈α⊂with␈α⊂the
␈↓formal␈αparameters␈αof␈α
␈↓αf␈↓(i.e.␈α␈↓αx:7␈α;y:4␈↓)␈α
and␈αevaluate␈αthe␈α
body␈αof␈α␈↓αf␈↓␈α
(i.e. ␈↓α7␈↓π2␈↓α + 4 = 57␈↓);␈αcall␈αby␈α
name␈αsays
␈↓␈↓↓42  Evaluation␈↓ 44.1␈↓


␈↓pass␈α∞the␈α∞unevaluated␈α∞actual␈α∞parameters␈α∞to␈α∞the␈α∞function,␈α∞then␈α∞perhaps␈α∞evaluate.␈α∂ I.e.␈α∞␈↓α(3+4)␈↓π2␈↓α + 2*2␈↓
␈↓arises␈α
(which␈α
also␈α
evaluates␈α
to␈α
␈↓α57␈↓).␈α
We␈α
will␈α
say␈α
more␈α
about␈α
other␈α
styles␈α
of␈α
evaluation␈αlater.␈α
However
␈↓most␈α
of␈α
this␈α
section␈α
will␈α
be␈α
restricted␈α
to␈α
call-by-value.

␈↓Notice␈αthat␈αthe␈αprocess␈αof␈αevaluation␈αby␈αvalue␈αis␈αrecursive.␈α That␈αis,␈αit␈αgoes␈αsomething␈αlike␈αthis:␈αIf
␈↓the␈α
expression␈αto␈α
be␈α
evaluated␈αis␈α
a␈α
constant␈αthen␈α
the␈α
value␈α(of␈α
the␈α
expression)␈αis␈α
that␈αconstant.␈α
 (the
␈↓value␈αof␈α␈↓α3␈↓␈α
is␈α␈↓α3␈↓).␈α If␈αthe␈α
expression␈αis␈αa␈αvariable␈α
the␈αsee␈αwhat␈αthe␈α
current␈αvalue␈αassociated␈αwith␈α
that
␈↓variable␈α∂is.␈α∂(Within␈α∞the␈α∂evaluation␈α∂of␈α∂say␈α∞␈↓αf[3;4]␈↓␈α∂where␈α∂␈↓αf[x;y] <= x␈↓π2␈↓α + y␈α∞␈↓the␈α∂current␈α∂value␈α∂of␈α∞the
␈↓variable␈α␈↓αx␈↓␈αis␈α␈↓α3␈↓.)␈αThe␈αonly␈αother␈αkind␈αof␈αexpression␈αthat␈αwe␈αcan␈αhave␈αis␈αa␈αfunction␈αname␈αfollowed
␈↓by␈α
arguments.␈α
(Say␈α
␈↓αf[3;4]␈↓).␈α
 In␈α
this␈α
case␈α
we␈α
evaluate␈α
the␈α
arguments,␈α
that␈α
is,␈α
we␈α
␈↓↓recur␈↓␈α
and␈α
then␈α
apply
␈↓the␈α⊂definition␈α⊂of␈α∂the␈α⊂function␈α⊂to␈α⊂those␈α∂evaluated␈α⊂arguments.␈α⊂ How␈α∂do␈α⊂you␈α⊂apply␈α⊂the␈α∂evaluated
␈↓arguments␈α
to␈α
the␈α
function␈α
definition?␈α
 You␈α
associate␈α
or␈α
bind␈α
the␈α
formal␈α
parameters␈α
(or␈α
variables)␈α
of
␈↓the␈αdefinition␈αto␈αthe␈αvalues␈αof␈αthe␈αactual␈αparameters.␈α Then␈αyou␈αevaluate␈αthe␈αbody␈αof␈αthe␈αfunction
␈↓in␈α∞this␈α∞new␈α
environment.␈α∞ Why␈α∞all␈α∞the␈α
emphasis␈α∞on␈α∞the␈α
mechanism␈α∞of␈α∞function␈α∞evaluation?␈α
 For
␈↓one␈α∃reason␈α∀implementation␈α∃of␈α∃programming␈α∀languages␈α∃requires␈α∀careful␈α∃consideration␈α∃of␈α∀this
␈↓problem.␈α⊗ If␈α↔you␈α⊗understand␈α⊗the␈α↔formal␈α⊗model␈α↔of␈α⊗LISP␈α⊗and␈α↔the␈α⊗corresponding␈α↔model␈α⊗of
␈↓implementation,␈αyou␈αshould␈αhave␈αan␈αexceptionally␈αclear␈αpicture␈αof␈αthe␈αproblems␈αof␈αimplementation
␈↓of␈α
languages,␈α
and␈α
you␈α
will␈α
understand␈α
many␈α
of␈α
the␈α
problems␈α
of␈α
semantics␈α
of␈α
languages.

␈↓How␈α
do␈αwe␈α
transcribe␈αthe␈α
evaluation␈α
of␈αarithmetic␈α
functions␈αto␈α
LISP␈α
functions.␈α It's␈α
easy.␈α First,␈α
the
␈↓class␈αof␈αLISP␈α
constants␈αis␈αlarger␈α
(than␈αthe␈αintegers)␈α
What␈αare␈αthe␈α
constants␈αof␈αLISP?␈α
They're␈αjust
␈↓the␈α∞Sexprs.␈α∞(The␈α∞value␈α∞of␈α∞␈↓α(A . B)␈↓␈α∞is␈α∞␈↓α(A . B)␈↓␈α∞---␈α
just␈α∞like␈α∞the␈α∞value␈α∞of␈α∞␈↓α3␈↓␈α∞is␈α∞␈↓α3␈↓␈α∞␈↓π 4␈↓).␈α∞ The␈α
additional
␈↓artifact␈α∪of␈α∪LISP␈α∪which␈α∪we␈α∪have␈α∪to␈α∪talk␈α∪about␈α∪with␈α∪respect␈α∪to␈α∪evaluation␈α∪is␈α∪the␈α∩conditional
␈↓expression.␈αBut␈αclearly␈αits␈αevaluation␈αcan␈αalso␈αbe␈αprecisely␈αspecified.␈αSee␈αpage␈α10.␈α In␈αmore␈αspecific
␈↓detail␈α
here␈α
is␈α
some␈α
of␈α
the␈α
structure␈α
of␈α
the␈α
LISP␈α
evaluation␈α
mechanism:

␈↓If␈α
the␈α
expression␈α
to␈α
be␈α
evaluated␈α
is␈α
a␈α
constant␈α
then␈α
the␈α
value␈α
is␈α
that␈α
constant.

␈↓If␈α
the␈α
expression␈α
is␈α
a␈α
variable␈α
find␈α
its␈α
value␈α
in␈α
the␈α
current␈α
environment.

␈↓If␈αthe␈αexpression␈αis␈αa␈αconditional␈αthen␈αit␈αis␈αof␈αthe␈αform␈α␈↓α[p␈↓β1␈↓α␈α→␈αe␈↓β1␈↓α;␈αp␈↓β2␈↓α␈α→␈αe␈↓β2␈↓α;␈α...␈αp␈↓βn␈↓α␈α→␈αe␈↓βn␈↓].␈α Evaluate␈αit
␈↓using␈α
the␈α
semantics␈α
defined␈α
on␈α
page␈α
10.

␈↓If␈α
the␈α
expression␈α
is␈α
of␈α
the␈α
form:␈α
function␈α
name␈α
followed␈α
by␈α
arguments␈α
then:
␈↓␈↓ αλ␈↓↓1.␈↓ evaluate the arguments (from left to right)
␈↓␈↓ αλ␈↓↓2.␈↓ find the definition of the function
␈↓␈↓ αλ␈↓↓3.␈↓ associate the evaluated arguments with the formal parameters in the function definition
␈↓␈↓ αλ      That is modify the environment of variables their values.
␈↓␈↓ αλ␈↓↓4.␈↓ evaluate the body of the function.

␈↓So␈αwe␈α␈↓↓can␈↓␈αdescribe␈αthe␈αoutline␈αof␈αa␈αmechanical␈αprocedure␈αwhich␈αcan␈αbe␈αused␈αin␈αthe␈αevaluation␈αof
␈↓LISP␈αfunctions.␈α Recall␈αour␈αdiscussion␈αof␈αthe␈αdifferentiation␈αformulas␈αin␈αSection␈α3.5.␈α We␈αdescribed
␈↓the␈αmechanism␈αof␈αdifferentiating␈αas␈αa␈αrecursive␈αprocess.␈α Then␈αmechanized␈αthat␈αscheme␈αas␈αa␈αLISP

________________
␈↓␈↓π 4␈↓␈α
We␈α
are␈α
ignoring␈α
the␈α
distinction␈α
between␈α
the␈α
␈↓↓numeral␈↓␈α
␈↓α3␈↓␈α
and␈α
the␈α
␈↓↓number␈↓α␈α
3.␈↓
␈↓␈↓↓4.1␈↓ 	↑Introduction     43␈↓


␈↓function,␈αtranslating␈αthe␈αpolynomials␈αto␈αlist␈αnotation,␈αsince␈αthe␈αdomain␈αof␈αLISP␈αfunctions␈αis␈αSexprs.
␈↓We␈α⊃are␈α∩in␈α⊃a␈α∩similar␈α⊃situation␈α⊃here.␈α∩ It␈α⊃should␈α∩be␈α⊃clear␈α⊃that␈α∩we␈α⊃could␈α∩write␈α⊃a␈α∩LISP␈α⊃function
␈↓representing␈α⊗the␈α↔evaluation␈α⊗process␈α↔provided␈α⊗that␈α⊗we␈α↔can␈α⊗find␈α↔a␈α⊗representation␈α↔for␈α⊗LISP
␈↓expressions␈α⊂as␈α∂Sexpressions.␈α⊂This␈α∂mapping␈α⊂of␈α⊂LISP␈α∂expressions␈α⊂to␈α∂Sexprs␈α⊂is␈α∂our␈α⊂first␈α⊂order␈α∂of
␈↓business.␈α⊂We␈α∂will␈α⊂accomplish␈α∂this␈α⊂mapping␈α⊂by␈α∂using␈α⊂an␈α∂extension␈α⊂of␈α∂the␈α⊂scheme␈α⊂introduced␈α∂in
␈↓Section␈α
3.5.

␈↓This␈αwhole␈αprocess␈αof␈αmapping␈αLISP␈αexpressions␈αonto␈αsexprs,␈αand␈αwriting␈αa␈αLISP␈αfunction␈α
to␈αact
␈↓as␈αan␈αevaluator␈αmay␈αseem␈αa␈αbit␈αincestuous␈αor␈αdifficult␈αto␈αfathom.␈α First,␈αit␈αis␈αno␈αmore␈αobscure␈αthan
␈↓the␈αdifferentiation␈α
example.␈α It␈αis␈α
just␈αanother␈αinstance␈α
of␈αthe␈αdiagram␈α
of␈αpage␈α25,␈α
only␈αnow␈αwe␈α
are
␈↓applying␈αthe␈αprocess␈αto␈αLISP␈αitself.␈α The␈αeffect␈αis␈αto␈αforce␈αus␈αto␈αmake␈αprecise␈αexactly␈αwhat␈αis␈αmeant
␈↓by␈α
LISP␈α
evaluation.␈α
This␈α
precision␈α
will␈α
have␈α
many␈α
important␈α
ramifications.

␈↓Second,␈α∞we've␈α∞been␈α∞doing␈α∞evaluation␈α∞of␈α∞sexpr␈α∞representations␈α∞of␈α∞LISP␈α∞expressions␈α∞already.␈α∞ The
␈↓␈↓↓great␈α∪mother␈α∪of␈α∀all␈α∪functions␈↓␈α∪is␈α∀exactly␈α∪the␈α∪evaluation␈α∀mechanism␈α∪for␈α∪the␈α∀LISP␈α∪primitive
␈↓functions␈αand␈α
predicates,␈α␈↓αcar,␈αcdr,␈α
cons,␈αatom␈↓␈αand␈α
␈↓αeq␈↓␈αwhen␈αrestricted␈α
to␈αfunctional␈α
composition␈αand
␈↓constant␈α
arguments.␈α
 The␈α
␈↓↓great␈α
mother␈α
revisited␈↓␈α
is␈α
the␈α
extension␈α
to␈α
conditional␈α
expressions.

␈↓But␈α
let's␈α
stop␈α
for␈α
some␈α
examples␈α
of␈α
translating␈α
LISP␈α
functions␈α
into␈α
Sexpr␈α
notation.



␈↓␈↓ ∧0␈↓↓4.2  Sexpr translation of LISP expressions␈↓


␈↓␈↓ ∧H␈↓↓LISP expression␈↓ πXtranslation as Sexpr

␈↓↓␈↓we will represent numbers just as numbers, e.g.:
␈↓␈↓ ∧H␈↓α2␈↓ πX2

␈↓α␈↓we will translate identifiers to their upper-case counterpart. Thus:
␈↓␈↓α␈↓ ∧Hx␈↓ πXX
␈↓α␈↓ ∧Hy2␈↓ πXY2
␈↓α␈↓ ∧Hcar␈↓ πXCAR

␈↓α␈↓Now␈αwe've␈αgot␈αa␈αproblem:␈αWe␈αwish␈αto␈αmap␈αarbitrary␈αLISP␈αexpressions␈αto␈αSexpressions.␈αThe␈αLISP
␈↓expression,␈α⊃␈↓αx␈↓␈α⊃translates␈α⊃to␈α⊃␈↓αX␈↓;␈α∩␈↓αX␈↓␈α⊃is␈α⊃itself␈α⊃a␈α⊃LISP␈α⊃expression␈α∩(a␈α⊃constant);␈α⊃␈↓↓it␈↓␈α⊃must␈α⊃also␈α∩have␈α⊃a
␈↓translate.

␈↓We␈α∞must␈α∞be␈α∞a␈α∞little␈α∞careful␈α∞here.␈α∞When␈α∞we␈α∞write␈α∞Son␈α∞of␈α∞Great␈α∞Mother␈α∞we␈α∞will␈α∞give␈α∞it␈α∞an␈α
sexpr
␈↓representation␈α
of␈α
a␈α
form␈α
to␈α∞be␈α
evaluated.␈α
We␈α
might␈α
give␈α∞it␈α
the␈α
representation␈α
of␈α
␈↓αcar[x]␈↓␈α∞in␈α
which
␈↓case␈α∞the␈α∞value␈α∞computed␈α∞will␈α∞depend␈α∞on␈α∞the␈α∞current␈α∞value␈α∞bound␈α∞to␈α∞␈↓αx␈↓.␈α∞ We␈α∞might␈α∞also␈α∞give␈α
the
␈↓representation␈αof␈α␈↓αcar[X]␈↓;␈αin␈αthis␈αcase␈αwe␈αshould␈αexpect␈αto␈αbe␈αpresented␈αwith␈αan␈αerror␈αmessage.␈α Or,
␈↓for␈α∞example␈α∂some␈α∞function␈α∞␈↓αfoo␈↓␈α∂we␈α∞wish␈α∂to␈α∞write␈α∞may␈α∂return␈α∞the␈α∞sexpr␈α∂representation␈α∞of␈α∂a␈α∞LISP
␈↓form␈α⊗as␈α↔its␈α⊗value.␈α↔ Say␈α⊗␈↓αfoo[1]␈↓␈α↔returns␈α⊗the␈α⊗representation␈α↔of␈α⊗␈↓αcar[x]␈↓␈α↔and␈α⊗␈↓αfoo[2]␈↓␈α↔returns␈α⊗the
␈↓␈↓↓44  Evaluation␈↓ 24.2␈↓


␈↓representation␈α∞of␈α∞␈↓αcar[X]␈↓.␈α∞We␈α∞must␈α∞be␈α
able␈α∞to␈α∞distinguish␈α∞between␈α∞these␈α∞representations.␈α∞ That␈α
is,
␈↓given␈αthe␈α
representation,␈αthere␈α
should␈αbe␈α
exactly␈αone␈α
way␈αof␈α
interpreting␈αit␈α
as␈αa␈α
LISP␈αexpression.
␈↓The␈α∞mapping␈α∞must␈α∞be␈α∞1-1.␈α∞So␈α∞we␈α∂must␈α∞represent␈α∞␈↓αx␈↓␈α∞and␈α∞␈↓αX␈↓␈α∞as␈α∞␈↓↓different␈↓␈α∞sexprs.␈α∂ The␈α∞translation
␈↓scheme␈α
we␈α
pick␈α
is:␈α
for␈α
any␈α
sexpression,␈α
y,␈α
its␈α
translation␈α
is␈α
␈↓α(QUOTE␈↓␈α
y␈↓α)␈↓.
␈↓For example:

␈↓␈↓ ∧H␈↓αX␈↓ πX(QUOTE X)
␈↓α␈↓ ∧H(A . B)␈↓ πX(QUOTE (A . B))
␈↓α␈↓ ∧HQUOTE␈↓ πX(QUOTE QUOTE)


␈↓We␈αmust␈α
also␈αshow␈α
how␈αto␈α
map␈αexpressions␈αof␈α
the␈αform␈α
␈↓αf[e␈↓β1␈↓α ; ... ;e␈↓βn␈↓α]␈↓␈αonto␈α
sexprs.␈α We␈αhave␈α
already
␈↓seen␈α∩one␈α∩satisfactory␈α⊃mapping␈α∩for␈α∩functions␈α⊃in␈α∩prefix␈α∩form␈α⊃in␈α∩Section␈α∩3.5.␈α⊃ We␈α∩will␈α∩use␈α⊃that
␈↓mapping␈α
(called␈α
Cambridge␈α
Polish)␈α
here.␈α
That␈α
is:
␈↓␈↓ ∧H␈↓αf[e␈↓β1␈↓α;e␈↓β2␈↓α; ...e␈↓βn␈↓α]␈↓ πX(F e␈↓β1␈↓α' e␈↓β2␈↓α' ...e␈↓βn␈↓')
␈↓␈↓ ∧H␈↓ πX  where ␈↓αe␈↓βi␈↓' is the translate of ␈↓αe␈↓βi␈↓α
␈↓α␈↓Examples:␈↓α
␈↓α␈↓ ∧Hcar[x]␈↓ πX(CAR X)
␈↓α␈↓ ∧Hcar[X]␈↓ πX(CAR (QUOTE X))
␈↓α␈↓ ∧Hcons[cdr[(A . B)];x]␈↓ πX(CONS (CDR (QUOTE (A . B))) X)

␈↓α␈↓What's left?   conditional expressions. The mapping is self-explanatory.
␈↓␈↓ ∧H␈↓α[p␈↓β1␈↓α → e␈↓β1␈↓α; ... p␈↓βn␈↓α → e␈↓βn␈↓α]␈↓ πX(COND (p␈↓β1␈↓α' e␈↓β1␈↓α') ... (p␈↓βn␈↓α' e␈↓βn␈↓α'))
␈↓α␈↓and an example:␈↓α
␈↓α␈↓ ∧H[atom[x] →1; q[y] → X]␈↓ πX(COND ((ATOM X) 1) ((Q Y) X))


␈↓Notice␈α∩that␈α∩␈↓α(COND ( ... ))␈↓␈α∩and␈α∩␈↓α(QUOTE ... )␈↓␈α∩␈↓↓look␈↓␈α∩like␈α∩translates␈α∩of␈α∩function␈α∩calls␈α∩of␈α∩the␈α⊃form
␈↓␈↓αcond[ ... ]␈α∂␈↓␈α⊂and␈α∂␈↓αquote[ ... ]␈↓.␈α⊂This␈α∂is␈α⊂␈↓↓not␈↓␈α∂the␈α∂case,␈α⊂␈↓αQUOTE␈↓␈α∂and␈α⊂␈↓αCOND␈↓␈α∂are␈α⊂not␈α∂translates␈α⊂of␈α∂LISP
␈↓functions.␈α The␈α
constructs␈α␈↓αQUOTE␈↓␈α
and␈α␈↓αCOND␈↓␈α
do␈αnot␈α
use␈αcall-by-value␈α
evaluation␈αand␈αare␈α
handled
␈↓in␈α
a␈α
special␈α
way␈α
as␈α
we␈α
shall␈α
soon␈α
see.

␈↓Since␈α
␈↓αT␈↓␈α
and␈α
␈↓αNIL␈↓␈α
are␈α
used␈α
so␈α
frequently,␈α
we␈α
chose␈α
their␈α
translates␈α
to␈α
be␈α
␈↓αT␈↓␈α
and␈α
␈↓αNIL␈↓␈α
rather␈αthan
␈↓␈↓α(QUOTE T)␈↓␈α
and␈α
␈↓α(QUOTE NIL)␈↓.

␈↓You␈αshould␈αnow␈α
go␈αback␈αand␈αlook␈α
at␈αthe␈α␈↓↓tgm␈↓'s␈α
(Section␈α3.9)␈αagain␈αnow␈α
that␈αyou␈αknow␈α
what␈αthey
␈↓mean.␈α
 You␈αshould␈α
note␈αthat␈α
with␈αrespect␈α
to␈αatoms,␈α
the␈αgreat␈α
mothers␈αare␈α
only␈αdefined␈α
for␈α
␈↓αT␈↓␈αand
␈↓␈↓αNIL␈↓.␈α∂ Other␈α∂atoms␈α∂(which␈α⊂are␈α∂the␈α∂translates␈α∂of␈α∂variables)␈α⊂are␈α∂not␈α∂recognized␈α∂and␈α∂return␈α⊂a␈α∂h.s.
␈↓message.␈α The␈αquestion␈αof␈αvariables␈αrequires␈αsome␈αcareful␈αstudy.␈α Before␈αwe␈αdo␈αthat,␈αlet's␈αsee␈αwhat
␈↓the␈αgreat␈αmothers␈αactually␈αaccomplish.␈α First,␈α
tgm's␈αexplicitly␈αtell␈αyou␈αwhat␈αthe␈αmeaning␈α
of␈αvarious
␈↓subsets␈α
of␈αLISP␈α
is.␈α That␈α
is,␈αthe␈α
tgms␈αexplicitly␈α
codify␈αthe␈α
semantics␈α(or␈α
meaning)␈αof␈α
LISP.␈α Next,␈α
if
␈↓you␈α
are␈α
able␈α
to␈α
code␈α
tgm␈α
on␈α
a␈α
machine,␈α
then␈α
you␈α
can␈α
input␈α
the␈α
Sexpr␈α
form␈α
of␈α
LISP␈αexpressions
␈↓and␈α
they␈α
will␈α
be␈α
evaluated␈α
for␈α
you.␈α
 ␈↓↓Great␈α
mother␈α
does␈α
it␈α
all.␈↓

␈↓The␈αpart␈αof␈αour␈αinformal␈αargument␈αwhich␈αis␈αclearly␈αmissing␈αin␈αthe␈αtgms␈αis␈αthe␈αdetails␈αof␈αhandling
␈↓␈↓↓4.2␈↓ πβSexpr translation of LISP expressions     45␈↓


␈↓variables␈α∞and␈α∞the␈α∞names␈α∞of␈α∂functions.␈α∞ That␈α∞is,␈α∞how␈α∞do␈α∂we␈α∞describe␈α∞the␈α∞binding␈α∞of␈α∂variables␈α∞to
␈↓values␈αas␈αa␈αmechanical␈αprocess␈αand,␈αgiven␈αthe␈αname␈αof␈αa␈αdefined␈αfunction,␈αhow␈αdo␈αwe␈αretrieve␈αthe
␈↓actual␈α
definition?␈α
 These␈α
are␈α
actually␈α
the␈α
same␈α
problem:␈α
that␈α
of␈α
symbol␈α
table␈α
manipulations.



␈↓␈↓ ∧q␈↓↓4.3  A first look at symbol tables␈↓


␈↓One␈α
of␈α
the␈α
distinguishing␈α
features␈α
of␈α
Computer␈α
Science␈α
is␈α
its␈α
reliance␈α
on␈α
the␈α
ability␈α
to␈α
store␈α
and
␈↓recover␈α∂information.␈α∂ Any␈α∂formalism␈α∞which␈α∂proports␈α∂to␈α∂address␈α∞itself␈α∂to␈α∂Computer␈α∂Science␈α∞must
␈↓take␈αthis␈α
into␈αaccount.␈α
 In␈αparticular␈α
we␈αmust␈α
be␈αable␈α
to␈αhandle␈α
the␈αeffect␈α
of␈αassignment␈αor␈α
binding,
␈↓that␈α∂is,␈α∂the␈α⊂association␈α∂of␈α∂a␈α⊂value␈α∂with␈α∂a␈α∂name␈α⊂in␈α∂an␈α∂environment.␈α⊂ A␈α∂common␈α∂device␈α⊂used␈α∂to
␈↓accomplish␈α∞this␈α∂is␈α∞a␈α∂symbol␈α∞table.␈α∂In␈α∞essence,␈α∂a␈α∞symbol␈α∞table␈α∂is␈α∞simply␈α∂a␈α∞set␈α∂of␈α∞ordered␈α∂pairs␈α∞of
␈↓objects␈α(a␈αfunction␈αor␈αrelation,␈αif␈αyou␈αwish).␈α One␈αof␈αthe␈αelements␈αof␈αeach␈αpair␈αis␈αa␈αname;␈αthe␈αother
␈↓is␈α
considered␈α
to␈α
be␈α
a␈α
value.␈α
 We␈α
will␈α
come␈α
back␈α
to␈α
symbol␈α
tables␈α
in␈α
a␈α
while␈α
to␈α
study␈α
the␈α
problems␈α
of
␈↓efficent␈α
storage␈α
and␈α
retrieval␈α
of␈α
information.␈α
It␈α
will␈αsuffice␈α
now␈α
simply␈α
to␈α
think␈α
of␈α
a␈α
symbol␈αtable␈α
as
␈↓represented␈α
in␈α
LISP␈α
by␈α
a␈α
list␈α
of␈α
dotted␈α
pairs␈α
---␈α
name␈α
dotted␈α
with␈α
value---.␈α
 These␈α
simple␈αsymbol
␈↓tables␈α
are␈α
also␈α
know␈α
as␈α
association␈α
lists␈α
or␈α
␈↓↓a-lists␈↓.

␈↓Since␈αwe␈αare␈αencoding␈αthe␈α␈↓↓set␈↓␈αof␈αobjects␈αas␈αa␈αlist,␈αwe␈αare␈αactually␈αencoding␈αit␈αas␈αa␈α␈↓↓sequence␈↓.␈αIt␈αwill
␈↓be␈α
quite␈α
useful␈α
to␈α
exploit␈α
this␈α
ordering␈α
imposed␈α
in␈α
the␈α
sequence.

␈↓Recall␈αthat␈αwe␈αare␈αrepresenting␈αvariables␈αas␈αatoms;␈αthen␈αif␈α␈↓αx,␈αy,␈α␈↓and␈α␈↓αz␈↓␈αhad␈αcurrent␈αvalues␈α␈↓α2,␈α3,␈α␈↓and
␈↓␈↓α4␈↓,␈α
then␈α
a␈α
symbol␈α
table␈α
describing␈α
that␈α
fact␈α
could␈α
be␈α
encoded␈α
as:

␈↓␈↓ ¬=␈↓α((X . 2)(Y . 3)(Z . 4)) .␈↓ 

␈↓In␈α
the␈α
sequel,␈αwe␈α
will␈α
need␈α
to␈αadd␈α
elements␈α
to␈αa␈α
table␈α
and,␈α
given␈αa␈α
name,␈α
find␈α
the␈αcorresponding
␈↓value.␈α
 Here's␈α
a␈α
simple␈α
symbol-table-searching␈α
function,␈α
␈↓αassoc␈↓:

␈↓α␈↓ αXassoc[x;l] <=␈↓ ∧H[null[l] → NIL;
␈↓α␈↓ αX␈↓ ∧H eq[caar[l];x] → car[l];
␈↓α␈↓ αX␈↓ ∧H T → assoc[x;cdr[l]]]

␈↓α␈↓e.g.␈↓α
␈↓α␈↓ ∧Qassoc[Y;((X . 2)(Y . 3)(Z . 4))] = (Y . 3)
␈↓α␈↓ ∧\assoc[U;((X . 2)(Y . 3)(Z . 4))] = NIL


␈↓␈↓αassoc␈↓␈αwill␈αexamine␈αthe␈α
table␈αfor␈αleft␈αto␈α
right,␈αlooking␈αfor␈αthe␈α
first␈αpair␈αwhose␈α␈↓αcar␈↓-part␈α
matches␈αthe
␈↓given␈αatom.␈αIf␈αsuch␈αa␈αpair␈αis␈αfound,␈αthen␈αthat␈αpair␈αis␈αreturned.␈α If␈α␈↓↓no␈↓␈αsuch␈αpair␈αis␈αfound,␈αthe␈α
result
␈↓is␈α
␈↓αNIL␈↓.␈α
 Notice␈α
there␈α
may␈α
be␈α
more␈α
than␈α
one␈α
pair␈α
with␈α
the␈α
same␈α
␈↓αcar␈↓-part;␈α
only␈α
the␈α
␈↓↓first␈↓␈α
is␈α
found.
␈↓␈↓↓46  Evaluation␈↓ /4.4␈↓


␈↓␈↓ ∧l␈↓↓4.4  Introduction to ␈↓αλ␈↓↓-expressions␈↓α


␈↓How␈α⊂about␈α⊃adding␈α⊂things␈α⊂to␈α⊃tables?␈α⊂ We␈α⊂will␈α⊃see␈α⊂that␈α⊂␈↓αcons␈↓␈α⊃plus␈α⊂the␈α⊂recursion␈α⊃mechanism␈α⊂will
␈↓automatically␈α⊂update␈α⊂the␈α⊃symbol␈α⊂table␈α⊂for␈α⊃␈↓↓Great-mother␈↓␈α⊂when␈α⊂we␈α⊂call␈α⊃a␈α⊂function.␈α⊂ We␈α⊃will␈α⊂be
␈↓adding␈α
entries␈αto␈α
the␈α
symbol␈αtable␈α
when␈α
we␈αcall␈α
a␈αfunction,␈α
binding␈α
the␈αformal␈α
parameters␈α
to␈αthe
␈↓actual␈α
parameters.␈α
Intuitively,␈α
when␈α
we␈α
call␈α
␈↓αf[x;y]␈α
<=␈α∞(x*y␈α
+␈α
y)␈α
␈↓in␈↓α␈α
f[2;3]␈↓,␈α
the␈α
dotted␈α
pairs␈α
␈↓α(X␈α∞.␈α
2)
␈↓α␈↓and␈α
␈↓α␈α
(Y␈α
.␈α
3)␈↓␈α
should␈α
find␈α
their␈α
way␈α
into␈α
the␈α
table.

␈↓Besides␈α∪the␈α∪binding␈α∪of␈α∩formal␈α∪parameters␈α∪to␈α∪actual␈α∩values,␈α∪there␈α∪is␈α∪another␈α∪binding␈α∩process
␈↓occurring␈αin␈αLISP.␈α Before␈αwe␈αcan␈αcall␈αthe␈α
function␈αnamed␈α␈↓αf␈↓,␈αits␈αdefinition␈αmust␈αbe␈αin␈α
the␈αsymbol
␈↓table;␈α∞so␈α∞there␈α∞should␈α∞be␈α∞an␈α∞entry␈α∞(dotted␈α∞pair)␈α∞with␈α∞name-part␈α∞␈↓αF␈↓␈α∞and␈α∞a␈α∞value-part␈α
representing
␈↓␈↓α(x*y␈α
+␈α
y)␈↓.␈α
 An␈α
obvious␈α
representation␈α
is␈α
perhaps,

␈↓␈↓ ¬0␈↓α(PLUS (TIMES X Y)Y)␈↓ 

␈↓That's␈α∨not␈α∨quite␈α∨good␈α≡enough.␈α∨If␈α∨we␈α∨simply␈α∨associate␈α≡the␈α∨name␈α∨␈↓αF␈↓␈α∨with␈α∨the␈α≡list
␈↓␈↓α(PLUS (TIMES X Y) Y)␈↓,␈α
we␈α
will␈αhave␈α
lost␈α
some␈α
crucial␈αinformation.␈α
 Namely,␈α
the␈αrelation␈α
between
␈↓the␈α↔variables␈α↔␈↓αx␈↓␈α↔and␈α⊗␈↓αy␈↓␈α↔and␈α↔the␈α↔body␈α⊗of␈α↔the␈α↔function␈α↔will␈α⊗not␈α↔be␈α↔present.␈α↔For␈α⊗example,
␈↓␈↓αf[y;x] <= (x*y +y)␈↓␈α∩is␈α∩␈↓↓not␈↓␈α∩the␈α∩same␈α∩function,␈α∩but␈α∩in␈α∩our␈α∩naive␈α∩translation␈α∩they␈α∩have␈α∪the␈α∩same
␈↓representation.␈α
 The␈α
solution␈α
is␈α∞obvious:␈α
we␈α
must␈α
associate␈α
the␈α∞list␈α
of␈α
formal␈α
parameters␈α∞with␈α
the
␈↓body␈α
of␈α
the␈α
definition.␈α
 Perhaps,

␈↓␈↓ ∧x␈↓α((X Y)(PLUS (TIMES X Y) Y)).␈↓ 

␈↓This␈α∂is␈α∂the␈α∂right␈α∂approach␈α∂and␈α∂it␈α∂can␈α∞be␈α∂made␈α∂precise.␈α∂ There␈α∂is␈α∂a␈α∂very␈α∂elegant␈α∂and␈α∞beautiful
␈↓theory␈αbehind␈αthis␈αintuitive␈αdiscussion.␈α We␈αwill␈αsketch␈αa␈αfew␈αparts␈αwhich␈αwill␈αbe␈αhelpful,␈αbut␈αfirst
␈↓let's␈α
see␈α
where␈α
we␈α
are:



␈↓␈↓ ¬%␈↓↓4.5  Review of evaluation␈↓


␈↓We␈α∞have␈α∞seen␈α∞that␈α∞it␈α∞is␈α∞possible␈α∞to␈α
mechanize␈α∞at␈α∞least␈α∞one␈α∞scheme␈α∞for␈α∞evaluation␈α∞of␈α∞functions␈α
--
␈↓left-to-right␈α
and␈αcall-by-value.␈α
 We␈αhave␈α
seen␈αthat␈α
it␈αis␈α
possible␈αto␈α
translate␈αLISP␈α
expressions␈αinto
␈↓Sexprs␈α
in␈α
such␈αa␈α
way␈α
that␈α
we␈αcan␈α
write␈α
a␈α
LISP␈αfunction␈α
which␈α
will␈α
act␈αas␈α
an␈α
evaluator␈α
for␈αsuch
␈↓translates.␈α
 In␈α∞the␈α
process␈α
we␈α∞have␈α
had␈α
to␈α∞mechanize␈α
the␈α
intuitive␈α∞devices␈α
we␈α
(as␈α∞humans)␈α
might
␈↓use␈α
to␈αrecall␈α
the␈α
definition␈αof␈α
functions␈α
and␈αto␈α
recall␈α
the␈αcurrent␈α
values␈α
of␈αvariables.␈α
 It␈α
was␈αsoon
␈↓clear␈αthat␈α
the␈αsame␈α
mechanism␈αcould␈αbe␈α
used:␈αthat␈α
of␈αsymbol␈αtables.␈α
 To␈αassociate␈α
a␈αvariable␈αwith␈α
a
␈↓value␈α
was␈α
easy:␈α
to␈α
associate␈α
a␈α
function␈α
name␈αwith␈α
its␈α
definition␈α
required␈α
some␈α
care.␈α
 That␈α
is,␈αpart␈α
of
␈↓the␈αdefiniton␈αof␈αa␈αfunction␈αinvolves␈αthe␈α
proper␈αassociation␈αof␈αformal␈αparameters␈αwith␈αthe␈α
body␈αof
␈↓the␈α∪definition.␈α∩(We␈α∪actually␈α∩need␈α∪to␈α∩be␈α∪a␈α∩bit␈α∪more␈α∩precise␈α∪than␈α∩this␈α∪in␈α∪describing␈α∩recursive
␈↓functions,␈α
but␈α
this␈α
is␈α
good␈α
enough␈α
for␈α
now.)␈α
The␈α
device␈α
we␈α
chose␈α
is␈α
called␈α
the␈α
␈↓↓lambda␈α
notation␈↓.
␈↓␈↓↓4.6␈↓ 	U␈↓αλ␈↓↓-expressions     47␈↓α


␈↓␈↓ ¬Y␈↓↓4.6  ␈↓αλ␈↓↓-expressions␈↓α


␈↓***more***

␈↓The␈α
λ-notation␈α
is␈α
a␈α
device␈α
invented␈α
by␈αthe␈α
logician,␈α
Alonzo␈α
Church,␈α
in␈α
conjunction␈α
with␈αhis␈α
studies
␈↓in␈α∩logic␈α∩and␈α∩foundations␈α∩of␈α∩mathematics.␈α∩ It␈α∩was␈α∩introduced␈α∩into␈α∩Computer␈α∩Science␈α∪by␈α∩John
␈↓McCarthy␈α
in␈α
the␈α
description␈α
of␈α
LISP.

␈↓As␈α
we␈α
intimiated␈α
previously,␈α
the␈αλ-notation␈α
is␈α
used␈α
in␈α
LISP␈α
to␈αallow␈α
us␈α
to␈α
attach␈α
names␈αto␈α
functions
␈↓in␈α
an␈α
unambiguous␈α∞manner.␈α
 We␈α
have␈α∞been␈α
very␈α
imprecise␈α∞when␈α
writing␈α
␈↓αf[x;y] <= (x*y + y)␈↓␈α∞as␈α
a
␈↓definition␈αof␈αthe␈αfunction␈α␈↓αf␈↓.␈αFirst␈αnote␈αthat␈α␈↓αf[x;y]␈↓␈αis␈α␈↓↓not␈↓␈αa␈αfunction,␈α␈↓αf␈↓␈αis.␈α To␈αsee␈αwhat␈α␈↓αf[x;y]␈↓␈αmeans
␈↓consider␈αthe␈αfollowing␈αexample.␈α When␈αwe␈αare␈αasked␈αto␈α
evaluate␈α␈↓αcar[(A␈α.␈αB)]␈↓␈αwe␈αsay␈αthe␈αvalue␈αis␈α
␈↓αA␈↓.
␈↓␈↓αcar[(A␈α.␈αB)]␈↓␈αis␈αan␈αexpression␈αto␈α
be␈αevaluated;␈αit␈αis␈αa␈αLISP␈αform.␈α If␈α
␈↓αcar[(A␈α.␈αB)]␈↓␈α␈↓↓is␈↓␈αa␈αform␈αthen␈αso␈α
is
␈↓␈↓αcar[x]␈↓;␈αonly␈αnow␈α
the␈αvalue␈αof␈α
the␈αform␈αdepends␈α
on␈αthe␈αcurrent␈α
value␈αassigned␈αto␈α
the␈αvariable␈α␈↓αx␈↓.␈α
 So
␈↓the␈α
␈↓↓function␈↓␈α
is␈α
␈↓αcar␈↓;␈α
the␈α
␈↓↓form␈↓␈α
is␈α
␈↓αcar[x]␈↓.␈α
 The␈α
function␈α
is␈α
␈↓αf␈↓;␈α
␈↓αf[x;y]␈↓␈α
is␈α
a␈α
form,␈α
as␈α
is␈α
␈↓αx*y + y␈↓.

␈↓Also␈αour␈α
notation␈αhas␈α
really␈αbeen␈αspecifying␈α
more␈αthan␈α
just␈αthe␈αname.␈α
 The␈αnotation␈α
specifies␈αthe
␈↓formal␈αparameters␈α(␈↓αx␈↓␈αand␈α␈↓αy␈↓)␈αand␈αthe␈αorder␈αin␈αwhich␈αwe␈αare␈αto␈αassociate␈αactual␈αparameters␈αin␈αa␈αcall
␈↓with␈α
the␈αformals␈α
of␈α
the␈αdefinition␈α
(␈↓αx␈↓␈α
with␈αthe␈α
first,␈α
␈↓αy␈↓␈αwith␈α
the␈α
second).␈α More␈α
subtly,␈α
the␈αnotation
␈↓tells␈α∞␈↓↓which␈↓␈α∞variables␈α∞in␈α∞the␈α∞function␈α∞body␈α
are␈α∞bound␈α∞variables.␈α∞ That␈α∞is,␈α∞which␈α∞variables␈α∞are␈α
to
␈↓expect␈αvalues␈α
when␈αthe␈α
function␈αis␈α
called.␈αFor␈αexample␈α
define␈α␈↓αg[x] <= (x*y + y)␈↓;␈α
then␈αthe␈α
value␈αof
␈↓␈↓αg[2]␈↓␈α
is␈α
well␈α
defined␈α
and␈α
is␈α
itself␈α
a␈α
function.

␈↓We␈α∞wish␈α∞to␈α∞have␈α∞a␈α∂notation␈α∞to␈α∞assign␈α∞all␈α∞this␈α∞other␈α∂information␈α∞with␈α∞the␈α∞function␈α∞body␈α∂so␈α∞that
␈↓function␈α⊂definitions␈α∂can␈α⊂be␈α⊂inserted␈α∂into␈α⊂the␈α⊂symbol␈α∂table␈α⊂as␈α⊂"values".␈α∂They␈α⊂will␈α⊂be␈α∂parametric
␈↓values,␈αbut␈αthey␈αwill␈αbe␈α
values.␈α The␈αλ-notation␈αperforms␈αthis␈α
task␈αby␈αpreceeding␈αthe␈αfunction␈α
body
␈↓with␈α
a␈α
list␈α
of␈α
the␈α
bound␈α
variables,␈αcalled␈α
␈↓↓lambda␈α
variables␈↓.␈α
 The␈α
list␈α
of␈α
variables␈α
is␈αusually␈α
referred
␈↓to␈α
as␈α
the␈α␈↓↓lambda␈α
list␈↓.␈α
 Then␈αthis␈α
resulting␈α
construct␈α
is␈αpreceeded␈α
by␈α
"λ["␈αand␈α
followed␈α
by␈α"]".␈α
 Thus
␈↓using␈α
the␈αabove␈α
example,␈α
the␈αfunction␈α
␈↓αf␈↓␈α
is␈αexactly␈α
the␈α
same␈αfunction␈α
as␈α
␈↓αλ[[x;y] x*y + y]␈↓.␈αWe␈α
actually
␈↓need␈α∞a␈α∞bit␈α∂more␈α∞than␈α∞λ-notation␈α∞to␈α∂specify␈α∞recursive␈α∞functions␈α∞in␈α∂a␈α∞natural␈α∞manner.␈α∂See␈α∞Section
␈↓4.11.

␈↓The␈α
λ-notation␈αintroduces␈α
nothing␈α
new␈αas␈α
far␈α
as␈αour␈α
intuitive␈α
binding␈αand␈α
evaluation␈αprocesses␈α
are
␈↓concerned,␈α∂it␈α∂only␈α∂makes␈α⊂these␈α∂operations␈α∂more␈α∂precise.␈α∂So␈α⊂to␈α∂evaluate␈α∂a␈α∂λ-expression,␈α⊂bind␈α∂the
␈↓evaluated␈α
actual␈α
parameters␈α
to␈α
the␈α
λ-variables␈α
and␈α
evaluate␈α
the␈α
function␈α
body.

␈↓Since␈α∞we␈α∞intend␈α∞to␈α∞include␈α∞λ-expressions␈α∞in␈α∞our␈α∞language␈α∞we␈α∞must␈α∞include␈α∞a␈α∞translation␈α∞of␈α
them
␈↓into␈α
sexpression␈α
form:
␈↓␈↓ ∧H␈↓↓LISP expression␈↓ πXtranslation as Sexpr␈↓
␈↓␈↓ ∧H␈↓αλ[[x␈↓β1␈↓α; ...; x␈↓βn␈↓α]␈↓λx␈↓α]␈↓ πX(LAMBDA (X␈↓β1␈↓α ...  X␈↓βn␈↓α) ␈↓translate of ␈↓λx␈↓α)

␈↓Thus␈α∂the␈α∂character,␈α∂λ,␈α⊂will␈α∂be␈α∂translated␈α∂to␈α∂␈↓αLAMBDA␈↓.␈α⊂␈↓αLAMBDA␈↓␈α∂is␈α∂therefore␈α∂a␈α∂kind␈α⊂of␈α∂prefix
␈↓operator␈α∪(like␈α∀␈↓αCOND␈↓)␈α∪which␈α∪allows␈α∀the␈α∪evaluator␈α∀to␈α∪recognize␈α∪the␈α∀occurrence␈α∪of␈α∀a␈α∪function
␈↓definition␈α
(just␈αas␈α
␈↓αCOND␈↓␈αis␈α
used␈αby␈α
the␈αevaluator␈α
to␈αrecognize␈α
the␈αoccurence␈α
of␈αa␈α
(translate␈α
of␈αa)
␈↓conditional␈α
expression).
␈↓␈↓↓48  Evaluation␈↓ 24.6␈↓


␈↓Here␈α
are␈α
some␈αexamples␈α
of␈α
␈↓αλ␈↓-expressions␈α
and␈αtheir␈α
evaluation,␈α
followed␈αby␈α
some␈α
examples␈α
of␈αthe
␈↓translation␈α
scheme:

␈↓␈↓ αX␈↓↓a.  ␈↓αλ[[x;y] x␈↓π2␈↓α +y][2;3] = 2␈↓π2␈↓α + 3 = 7
␈↓α␈↓ αX␈↓↓b.  ␈↓αλ[[x;y] cons[car[x];y]][(A . B); C] = (A . C)
␈↓α␈↓ αX␈↓↓c.  ␈↓αλ[[y] cdr[y]][(A B)] = (B)
␈↓α␈↓ αX␈↓↓d.  ␈↓αλ[[x] car[x]][λ[[y] cdr[y]][(A B)]]
␈↓α␈↓ αX     = λ[[x] car[x]][(B)]
␈↓α␈↓ αX     = B

␈↓α␈↓ αX␈↓↓e.  ␈↓αλ[[x;y] x␈↓π2␈↓α +y]  ==> (LAMBDA(X Y)(PLUS (EXPT X 2) Y))
␈↓α␈↓ αX␈↓↓f.  ␈↓αλ[[x;y] cons[car[x];y]]  ==> (LAMBDA(X Y)(CONS (CAR X) Y))

␈↓******MORE*******

␈↓Our␈α
LISP␈α
syntax␈α
equations␈α
should␈α
now␈α
be␈α
augmented␈α
to␈α
include:

␈↓<function>␈↓ αh::= λ[<varlist><form>]

␈↓<varlist>␈↓ αh::= [<variable>; ... ; <variable>]
␈↓␈↓ αh::= [ ]


␈↓One␈α⊃of␈α⊃the␈α⊂other␈α⊃more␈α⊃valuable␈α⊃application␈α⊂of␈α⊃λ-notation␈α⊃occurs␈α⊂in␈α⊃that␈α⊃nebulous␈α⊃area␈α⊂called
␈↓efficiency.␈α Efficiency␈αis␈αlike␈αstructured␈αprogramming␈α--␈αdifficult␈αto␈αdefine␈αbut␈αrecognizable␈αwhen␈αit
␈↓occurs.

␈↓Consider␈α
the␈α
following␈α
sketch␈α
of␈α
a␈α
function␈α
definition:

␈↓␈↓ ∧f␈↓αg <= λ[[x][p[lic[x]] → lic[x]; .... x ...]]␈↓. 

␈↓Where␈α␈↓αlic␈↓␈αmay␈αbe␈αa␈α␈↓↓l␈↓ong␈α␈↓↓i␈↓nvolved␈α␈↓↓c␈↓alculation.␈α We␈αcertainly␈αmust␈αcompute␈α␈↓αlic[x]␈↓␈α␈↓↓once␈↓.␈αBut␈αas␈α␈↓αg␈↓␈αis
␈↓defined,␈α
we␈α
would␈α
compute␈α␈↓αlic[x]␈↓␈α
␈↓↓again␈↓␈α
as␈α
e␈↓β1␈↓␈α
if␈αp␈↓β1␈↓␈α
is␈α
true.␈α
Since␈αour␈α
current␈α
LISP␈α
subset␈α
has␈αno
␈↓side␈α∞effects,␈α∞this␈α
second␈α∞calculation␈α∞is␈α∞unnecessary.␈α
␈↓αg␈↓␈α∞is␈α∞inefficient.␈α
Using␈α∞λ-expressions,␈α∞in␈α∞a␈α
style
␈↓called␈α
␈↓↓internal␈α
lambdas␈↓␈α
we␈α
can␈α
improve␈α
␈↓αg␈↓␈α
as␈α
follows:

␈↓Replace␈α
the␈α
body␈α
of␈α
␈↓αg␈↓␈α
with,

␈↓␈↓ ∧y␈↓αλ[y][p[y] → y; ... x ...][lic[x]]␈↓.  (*1*) 

␈↓Call␈α
this␈α
new␈α
function␈α
␈↓αg'␈↓:
␈↓␈↓ αX␈↓αg' <= λ[[x]
␈↓α␈↓ αX␈↓ ∧(λ[y][p[y] → y; ... x ... ][lic[x]]
␈↓α␈↓ αX␈↓ ∧(      ] ␈↓.
␈↓␈↓↓4.6␈↓ 	U␈↓αλ␈↓↓-expressions     49␈↓α


␈↓Now␈α
when␈α
␈↓αg'␈↓␈α
is␈α
called␈α
we␈α
evaluate␈α
the␈αactual␈α
parameter,␈α
binding␈α
it␈α
to␈α
␈↓αx␈↓,␈α
as␈α
always;␈α
and␈αevaluate␈α
the
␈↓body,␈α
(*1*).␈αEvaluation␈α
of␈α
(*1*)␈αinvolves␈α
calculation␈αof␈α
␈↓αlic[x]␈↓␈α
(␈↓↓once␈↓),␈αbinding␈α
the␈α
result␈αto␈α
␈↓αy␈↓.␈αWe␈α
then
␈↓evaluate␈αthe␈αbody␈α
of␈αthe␈αconditional␈α
expression␈αas␈αbefore.␈α If␈α
p␈↓β1␈↓␈α␈↓↓is␈↓␈αtrue,␈α
then␈αthis␈αdefinition␈α
of␈α␈↓αg'␈↓
␈↓involves␈α
one␈α
calculation␈α
of␈α
␈↓αlic[x]␈↓␈α
and␈α
two␈α
table␈αlook-ups␈α
(for␈α
the␈α
value␈α
of␈α
␈↓αy␈↓),␈α
rather␈α
than␈α
the␈αtwo
␈↓calculations␈α
of␈α
␈↓αlic[x]␈↓␈α
in␈α
␈↓αg␈↓.␈α
 More␈αconventional␈α
programming␈α
languages␈α
can␈α
obtain␈α
the␈α
same␈αeffect␈α
as
␈↓this␈α∞use␈α
of␈α∞internal␈α
lambdas␈α∞by␈α
assignment␈α∞of␈α
␈↓αlic[x]␈↓␈α∞to␈α
an␈α∞temporary␈α
variable.␈α∞We␈α∞will␈α
introduce
␈↓assignment␈α∞statements␈α∞in␈α∞LISP␈α∞in␈α∞Section␈α∞4.14.
␈↓↓␈↓ ε⊃Problems


␈↓I␈α
What␈α
is␈α
the␈α
difference␈α
between␈α
␈↓αλ[[␈α
]␈α
x*y␈α
+␈α
y]␈↓␈α
and␈α
␈↓αx*y␈α
+␈α
y␈↓?

␈↓****more␈α
probs****



␈↓␈↓ ∧t␈↓↓4.7  Mechanization of evaluation␈↓


␈↓As␈αwe␈αhave␈αsaid,␈α␈↓αtgmoaf␈↓␈α
and␈α␈↓αtgmoafr␈↓␈α(Section␈α3.9)␈αare␈α
evaluators␈αfor␈αsubsets␈αof␈αLISP.␈α Armed␈α
with
␈↓your␈αsymbol-table␈αmechanism␈αwe␈αcould␈αnow␈αextend␈αthe␈α␈↓↓great-mothers␈↓␈αto␈αhandle␈αvariable␈αlook-ups.
␈↓Rather␈α∞than␈α∞do␈α∞this␈α∞we␈α∞will␈α∞display␈α∞our␈α∞burgeoning␈α∞cleverness␈α∞and␈α∞make␈α∞a␈α∞total␈α∞revision␈α∞of␈α
the
␈↓structure␈α
of␈α
the␈α
evaluators.␈α
In␈α
making␈α
the␈α
revision,␈α
the␈α
following␈α
points␈α
should␈α
be␈α
remembered:

␈↓1.␈α
 Expressions␈α
(to␈αbe␈α
evaluated)␈α
can␈αcontain␈α
variables,␈α
both␈αsimple␈α
variables␈α
and␈αvariables␈α
naming
␈↓λ-expressions.␈αThus␈αevaluation␈αmust␈α
be␈αdone␈αwith␈αrespect␈αto␈α
an␈αenvironment␈αor␈αsymbol␈α
table.␈αWe
␈↓wish␈α
to␈α
recognize␈α
other␈α
(translates␈α
of)␈α
function␈α
names␈α
besides␈α
␈↓αCAR,␈α
CDR,␈α
CONS,␈α
EQ␈↓␈α
and␈↓α␈α
ATOM␈↓␈α
in
␈↓our␈αevaluator,␈αbut␈αexplicitly␈αadding␈αnew␈αdefinitions␈αto␈αthe␈αevaluator␈αin␈αthe␈αstyle␈αof␈αthe␈αrecognizers
␈↓for␈α
the␈αfive␈α
primitives␈αis␈α
a␈αbad␈α
idea.␈α
 Our␈αsymbol␈α
table␈αshould␈α
hold␈αthe␈α
function␈α
definitions␈αand
␈↓the␈αevaluator␈α
should␈αcontain␈α
the␈αgeneral␈α
schemes␈αfor␈α
finding␈αthe␈α
definitions,␈αbinding␈α
variables␈αto
␈↓values,␈αand␈αevaluating␈αthe␈αfunction␈α
body.␈α By␈αnow␈αyou␈αshould␈α
be␈αconvinced␈αthat␈αthis␈αprocess␈α
is␈αa
␈↓reasonably␈α
well␈α
defined␈α
task␈α
and␈α
could␈α
be␈α
written␈α
in␈α
LISP.

␈↓2.␈α All␈α
␈↓↓function␈↓␈αcalls␈αare␈α
to␈αbe␈αevaluated␈α
by-value.␈αHowever␈α
there␈αare␈αsome␈α
␈↓↓special␈αforms␈↓␈αwe␈α
have
␈↓seen␈αwhich␈αare␈αnot␈αevaluated␈αin␈αthe␈αnormal␈αmanner.␈α In␈αparticular,␈αconditional␈αexpressions,␈αquoted
␈↓expressions,␈α
and␈α
lambda␈α
expressions␈α
are␈α
handled␈α
differently.

␈↓Abstractly,␈α
we␈α
might␈α
write␈α
␈↓αsogm␈↓␈α
(son␈α
of␈α
great␈α
mother)␈α
as:

␈↓αsogm <= λ[[exp;environ]
␈↓α␈↓ α([isvar[exp] → value[exp;environ];
␈↓α␈↓ α( isconst[exp] → denote[exp];
␈↓α␈↓ α( iscond[exp] → evcond[exp;environ];
␈↓α␈↓ α( isfunc+args[exp] → applyfun[func[exp];list_of_sogmed_args[arg[exp];environ];environ]]
␈↓␈↓↓50  Evaluation␈↓ 24.7␈↓



␈↓α␈↓where:␈↓α
␈↓αapplyfn <= λ[[fn;args,environ]
␈↓α␈↓ β([iscar[fn] → car[arg␈↓β1␈↓α[args]];
␈↓α␈↓ β( iscons[fn] → cons[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓α␈↓ β(       ....               ....
␈↓α␈↓ β( islambda[fn] → sogm[body[fn];newenviron[vars[fn];args;environ]]
␈↓α␈↓ β(      ....          ..... ]]

␈↓α␈↓and:␈↓α
␈↓αdenote <= λ[[exp]
␈↓α␈↓ β([isnumber[exp] → exp;           ␈↓(see footnote page 42)␈↓α
␈↓α␈↓ β( istruth[exp] → exp;
␈↓α␈↓ β( isfalse[exp] → exp;
␈↓α␈↓ β( isSexpr[exp] → rep[exp]]


␈↓␈↓αlist_of_sogmed_args␈↓␈α↔is␈α↔a␈α↔function␈α↔to␈α↔perform␈α↔left-to-right␈α↔evaluation␈α↔of␈α↔the␈α_argument␈α↔list.
␈↓␈↓αnewenviron␈↓␈α∃is␈α∃a␈α∃function␈α∃to␈α∃manufacture␈α∀a␈α∃new␈α∃environment␈α∃reflecting␈α∃a␈α∃rebinding␈α∃of␈α∀the
␈↓λ-variables␈αto␈αthe␈αnew␈αvalues.␈α␈↓αfunc␈↓␈αis␈αa␈αselector␈αto␈αpick␈αout␈αthe␈αfunction␈αfrom␈αthe␈αrepresentation␈αof
␈↓the␈αform;␈α␈↓αarg␈↓␈αselects␈αthe␈αargument␈αlist␈αfrom␈αthe␈αrepresentation.␈αThis␈αabstract␈αsketch␈αis␈αnot␈αmeant␈αto
␈↓be␈α
complete,␈α
but␈α
rather␈α
only␈α
to␈α
whet␈α
the␈α
intuition.

␈↓With␈α
this␈αshort␈α
introduction␈αwe␈α
will␈αnow␈α
write␈α
a␈αmore␈α
general␈αevaluator␈α
which␈αwill␈α
handle␈αa␈α
larger
␈↓subset␈α
of␈α
LISP␈α
than␈α
the␈α
␈↓αtgm␈↓s.

␈↓The␈α
primary␈αfunction␈α
is␈αnamed␈α
␈↓αeval␈↓␈α(rather␈α
than␈α
Son␈αof␈α
Great␈αMother).␈α
It␈αwill␈α
take␈αtwo␈α
arguments;
␈↓the␈αfirst␈αis␈αa␈αrepresentation␈αof␈αan␈αexpression␈αto␈αbe␈αevaluated,␈αand␈αthe␈αsecond␈αis␈αa␈αrepresentation␈αof
␈↓a␈α∂symbol␈α∂table.␈α∂␈↓αeval␈↓␈α∂will␈α∂recognize␈α∂numbers,␈α∂the␈α∞constants␈α∂␈↓αT␈↓␈α∂and␈α∂␈↓αNIL␈↓,␈α∂and␈α∂if␈α∂presented␈α∂with␈α∞a
␈↓variable,␈α
will␈α
attempt␈α
to␈α
find␈α
the␈α
value␈α
of␈α
the␈α
variable␈α
in␈α
the␈α
symbol␈α
table.

␈↓␈↓αeval␈↓␈α⊂also␈α⊃handles␈α⊂the␈α⊃previously␈α⊂mentioned␈α⊃special␈α⊂forms.␈α⊃If␈α⊂␈↓αeval␈↓␈α⊃sees␈α⊂a␈α⊃conditional␈α⊂expression
␈↓(represented␈αby␈α␈↓α(COND␈α...)␈↓␈α)␈αthen␈αthe␈αbody␈αof␈αthe␈α␈↓αCOND␈↓␈αis␈αpassed␈αto␈αa␈αsubfunction␈αnamed␈α␈↓αevcond␈↓.
␈↓␈↓αevcond␈↓␈α⊂encodes␈α∂the␈α⊂semantics␈α⊂as␈α∂described␈α⊂on␈α⊂page␈α∂10.␈α⊂The␈α⊂special␈α∂form,␈α⊂␈↓αQUOTE␈↓,␈α⊂signifies␈α∂the
␈↓occurrence␈α
of␈α
a␈α
constant.␈α
Simply␈α
return␈α
that␈α
constant.␈α
 As␈α
far␈α
as␈α
this␈α
␈↓αeval␈↓␈α
is␈α
concerned,␈α
any␈αother
␈↓expression␈αis␈αa␈αfunction-call.␈αIn␈αthis␈αcase␈αwe␈α␈↓↓apply␈↓␈αthe␈αfunction␈αto␈αthe␈αlist␈αof␈αevaluated␈αarguments.
␈↓The␈α
function␈α
may␈α
either␈α
be␈α
a␈α
name␈α
or␈α
it␈α
may␈α
be␈α
a␈α
λ-expression.␈α
␈↓αapply␈↓␈α
handles␈α
both␈α
cases.
␈↓Here's the new ␈↓αeval␈↓:
␈↓αeval <= λ[[e;a]␈↓ β8[atom[e] →␈↓ ∧h[numberp[e] → e;
␈↓α␈↓ β8␈↓ ∧h eq[e;T] → T;
␈↓α␈↓ β8␈↓ ∧h eq[e;NIL] → NIL;
␈↓α␈↓ β8␈↓ ∧h T → cdr[assoc[e;a]]];
␈↓α␈↓ β8 atom[car[e]] →␈↓ ∧h[eq[car[e];QUOTE] → cadr[e];
␈↓α␈↓ β8␈↓ ∧h eq[car[e];COND] → evcond[cdr[e];a];
␈↓α␈↓ β8␈↓ ∧h T → apply[car[e];evlis[cdr[e];a];a]]
␈↓α␈↓ β8 T → apply[car[e];evlis[cdr[e];a];a]]]
␈↓␈↓↓4.7␈↓ λ
Mechanization of evaluation     51␈↓


␈↓α␈↓where:␈↓α

␈↓αevcond <= λ[[e;a][eval[caar[e];a] → eval[cadar[e];a];
␈↓α␈↓ β8 T → evcond[cdr[e];a]]]
␈↓α␈↓and,␈↓α

␈↓αevlis <= λ[[e;a][null[e] → NIL;
␈↓α␈↓ β8 T → cons[eval[car[e];a];evlis[cdr[e];a]]]]

␈↓The␈αsubfunctions,␈α
␈↓αevcond␈↓␈αand␈α␈↓αevlis␈↓,␈α
are␈αsimple.␈α ␈↓αevcond␈α
␈↓you've␈αseen␈αbefore␈α
in␈α␈↓αtgmoafr␈↓;␈α
␈↓αevlis␈↓␈αsimply
␈↓manufactures␈αa␈αnew␈αlist␈αconsisting␈αof␈αthe␈αresults␈αof␈αevaluating␈α(from␈αleft␈αto␈αright)␈αthe␈αelements␈αof␈α␈↓αe␈↓,
␈↓using␈α
the␈α
symbol␈α
table,␈α
␈↓αa␈↓,␈α
where␈α
necessary.

␈↓The␈α
interesting␈α
innovations␈α
appear␈α
in␈α
the␈α
function,␈α
␈↓αapply␈↓.␈α
 ␈↓αapply␈↓␈α
takes␈α
three␈α
arguments:␈αa␈α
function,
␈↓a␈α∂list␈α∂of␈α∞evaluated␈α∂arguments,␈α∂and␈α∂a␈α∞symbol␈α∂table.␈α∂␈↓αapply␈↓␈α∞explicitly␈α∂recognizes␈α∂the␈α∂five␈α∞primitive
␈↓functions␈α
␈↓αCAR,␈α
CDR,␈αCONS,␈α
EQ,␈α
␈↓and␈↓α␈αATOM␈↓.␈α
If␈α
the␈αfunction␈α
name␈α
is␈αa␈α
variable,␈α
(represented␈αas
␈↓an␈α
atom)␈αthen␈α
the␈αdefinition␈α
is␈α
located␈αin␈α
the␈αsymbol␈α
table␈α
by␈α␈↓αeval␈↓␈α
and␈αapplied␈α
to␈α
the␈αarguments.
␈↓Otherwise␈α∂the␈α∂function␈α∂must␈α∂be␈α∂a␈α∂λ-expression.␈α∞ This␈α∂is␈α∂where␈α∂things␈α∂get␈α∂interesting.␈α∂We␈α∞know
␈↓what␈αwe␈αmust␈α
do:␈αevaluate␈αthe␈α
body␈αof␈αthe␈α
λ-expression␈αafter␈αbinding␈α
the␈αformal␈αparameters␈αof␈α
the
␈↓λ-expression␈α∞to␈α∞the␈α∞evaluated␈α∞arguments.␈α∞How␈α∞do␈α∞we␈α∞bind?␈α∞ We␈α∞add␈α∞variable-value␈α∞pairs␈α∞to␈α
the
␈↓symbol␈αtable.␈αWe␈αwill␈α
define␈αa␈αsubfunction,␈α␈↓αpairlis␈↓,␈α
to␈αperform␈αthe␈αbinding.␈α
 Then␈αall␈αthat␈αis␈αleft␈α
to
␈↓do␈α
is␈α
give␈α
the␈α
function␈α
body␈α
and␈α
the␈α
new␈α
symbol␈α
table␈α
to␈α
␈↓αeval␈↓.␈α
 Here␈α
is␈α
␈↓αapply␈↓:

␈↓αapply <= λ[[fn;x;a][atom[fn] → [␈↓ ∧xeq[fn;CAR] → caar[x];
␈↓α␈↓ βx␈↓ ∧xeq[fn;CDR] → cdar[x];
␈↓α␈↓ βx␈↓ ∧xeq[fn;CONS] → cons[car[x];cadr[x]];
␈↓α␈↓ βx␈↓ ∧xeq[fn;ATOM] → atom[car[x]];
␈↓α␈↓ βx␈↓ ∧xeq[fn;EQ] → eq[car[x];cadr[x]];
␈↓α␈↓ βx␈↓ ∧xT → apply[eval[fn;a];x;a]];
␈↓α␈↓ βxeq[car[fn];LAMBDA] → eval[caddr[fn];pairlis[cadr[fn];x;a]] ]]

␈↓αpairlis <= λ[[v;w;x][null[w] → x;
␈↓α␈↓ βxT → cons[cons[car[v];car[w]];pairlis[cdr[v];cdr[w];x]]]]


␈↓So␈α
the␈α
only␈α
really␈α
new␈αdevelopment␈α
is␈α
in␈α
the␈α
λ-binding␈αprocess.␈α
 Notice␈α
that␈α
␈↓αpairlis␈↓␈α
makes␈α
a␈αnew
␈↓symbol␈α
table␈α
by␈αconsing␈α
new␈α
pairs␈α
onto␈αthe␈α
front␈α
of␈αthe␈α
old␈α
symbol␈α
table;␈αand␈α
recall␈α
that␈α␈↓αassoc␈↓␈α
finds
␈↓the␈α
␈↓↓first␈↓␈α
matching␈α
pair␈α
in␈α
the␈α
symbol␈α
table.␈α
␈↓↓This␈α
is␈α
important.␈↓
␈↓␈↓ ε⊃␈↓↓Problems

␈↓↓I  ␈↓Complete the abstract versions of ␈↓αsogm␈↓ and Co.
␈↓␈↓↓52  Evaluation␈↓ 14.8␈↓


␈↓␈↓ ¬?␈↓↓4.8  Examples of ␈↓αeval␈↓↓␈↓α


␈↓After␈αthis␈αonslaught,␈αsome␈αexamples␈αare␈αin␈αorder.␈α Let's␈αevaluate␈α␈↓αf[2;3]␈↓␈αwhere␈α␈↓αf␈α<=␈αλ[[x;y]␈αx␈↓π2␈↓α␈α+␈αy].␈↓
␈↓After␈α
appropriate␈α
translation␈α
this␈α
is␈α
equivalent␈α
to␈α
evaluating:

␈↓␈↓ β?␈↓αeval[(F 2 3);((F . (LAMBDA(X Y)(PLUS (EXPT X 2)Y))))]␈↓ 

␈↓Notes:

␈↓␈↓↓1.␈↓␈α
␈↓α((F␈α
.␈α
(LAMBDA␈α
(X␈α
Y)␈α
...␈α
)))␈α
=␈α
((F␈α
LAMBDA␈α
(X␈α
Y)␈α
...␈α
))␈α
␈↓

␈↓␈↓↓2.␈↓␈αSince␈αthe␈αsymbol␈αtable,␈α␈↓α((F␈α...))␈↓␈αoccurs␈αso␈αfrequently␈αin␈αthe␈αfollowing␈αtrace,␈αwe␈αwill␈αabbreviate␈αit
␈↓as␈α∂␈↓αst␈↓.␈α⊂Note␈α∂that␈α∂we␈α⊂have␈α∂no␈α∂mechanism␈α⊂yet␈α∂for␈α∂permanently␈α⊂increasing␈α∂the␈α∂repetoire␈α⊂of␈α∂known
␈↓functions.␈α
 We␈α
must␈α
resort␈α
to␈α
the␈α
subterfuge␈α∞of␈α
initializing␈α
the␈α
symbol␈α
table␈α
to␈α
get␈α
the␈α∞function␈α
␈↓αf␈↓
␈↓defined.

␈↓␈↓↓3.␈↓␈α∂For␈α∂this␈α∂example␈α∂we␈α∂must␈α∂assume␈α∂that␈α∂+␈α∂and␈α∂↑␈α∂(exponentiation)␈α∂are␈α∂known␈α∂functions.␈α∞ Thus
␈↓apply␈α
would␈α
have␈α
to␈α
contain␈α
recognizers␈α
for␈α
␈↓αPLUS␈↓␈α
and␈α
␈↓αTIMES␈↓:
␈↓α        ...atom[fn] → [eq[fn];PLUS] → +[car[x];cadr[x]];
␈↓α                        eq[fn;EXPT] → ↑[car[x];cadr[x]];
␈↓α                         ...
␈↓␈↓↓4.8␈↓ 	!Examples of ␈↓αeval␈↓↓     53␈↓α


␈↓So ␈↓αeval[(F 2 3);st]
␈↓α␈↓ αX= apply[car[(F 2 3)]; evlis[cdr[(F 2 3)];st];st]
␈↓α␈↓ αX= apply[F;evlis[(2 3);st];st]
␈↓α␈↓ αX= apply[F;(2 3);st]
␈↓α␈↓ αX= apply[eval[F;st];(2 3);st]
␈↓α␈↓ αX= apply[(LAMBDA(X Y)(PLUS (EXPT X 2) Y)); (2 3);st]

␈↓α␈↓ αX= eval[caddr[(LAMBDA(X Y)(PLUS (EXPT X 2) Y))];
␈↓α␈↓ αX        pairlis[cadr[(LAMBDA(X Y)(PLUS (EXPT X 2) Y))];(2 3);st]]

␈↓α␈↓ αX= eval[(PLUS (EXPT X 2) Y);pairlis[(X Y);(2 3);st]]
␈↓α␈↓ αX= eval[(PLUS (EXPT X 2) Y);((X . 2)(Y . 3)(F LAMBDA(X Y) ...))]

␈↓α␈↓ αX= apply [PLUS; evlis[((EXPT X 2) Y);((X . 2)(Y . 3)..)];((X . 2)...)]

␈↓α␈↓ αX     ␈↓Let's do a little of:␈↓α  evlis[((EXPT X 2)Y);((X . 2)(Y . 3)...)]
␈↓α␈↓ αX␈↓ ∧H= cons[eval[(EXPT X 2);((X . 2)(Y . 3) ...)];
␈↓α␈↓ αX␈↓ ∧H         evlis[(Y);((X . 2) ...)]]
␈↓α␈↓ αX␈↓ ∧H= cons[apply[EXPT;evlis[(X 2);((X . 2)...)];((X . 2) ...]
␈↓α␈↓ αX␈↓ ∧H         evlis[(Y); ...]]

␈↓α␈↓ αX␈↓ ∧H= cons[apply[EXPT;(2 2);((X . 2) ...];evlis[(Y); ...]]
␈↓α␈↓ αX␈↓ ∧H= cons[↑[car[(2 2)];cadr[(2 2)]];evlis[(Y); ... ]]
␈↓α␈↓ αX␈↓ ∧H= cons[↑[2;2];evlis[(Y); ... ]]
␈↓α␈↓ αX␈↓ ∧H= cons[4;evlis[(Y);((X . 2)(Y . 3) ....)]]
␈↓α␈↓ αX␈↓ ∧H= cons[4;cons[eval[Y;((X .2) ...)]; evlis[NIL;(( ...))]]]
␈↓α␈↓ αX␈↓ ∧H= cons[4;cons[3;NIL]]
␈↓α␈↓ αX␈↓ ∧H= (4 3)  ␈↓ which you knew anyway.

␈↓Now back to apply:
␈↓α␈↓ αX= apply[PLUS;(4 3);((X . 2)(Y . 3) ... )]
␈↓α␈↓ αX= +[4;3]
␈↓α␈↓ αX= 7    ␈↓which you also knew.

␈↓It␈αshould␈αnow␈αbe␈αclear␈αthat␈α␈↓αeval␈↓␈αand␈αfriends␈α␈↓↓do␈↓␈αbegin␈αto␈αperform␈αas␈αyou␈αwould␈αexpect.␈α It␈αperhaps
␈↓is␈α∞not␈α
clear␈α∞that␈α∞a␈α
simpler␈α∞scheme␈α∞might␈α
do␈α∞as␈α∞well.␈α
 In␈α∞particular,␈α∞the␈α
complexity␈α∞of␈α∞the␈α
symbol
␈↓table␈α∞mechanism␈α∞which␈α∞we␈α∞claimed␈α∞was␈α∞so␈α∞important␈α∞has␈α∞not␈α∞been␈α∞exploited.␈α∞The␈α∞next␈α
example
␈↓will␈α
indeed␈α
show␈α
that␈α
a␈α
scheme␈α
like␈α
ours␈α
is␈α
necessary␈α
to␈α
keep␈α
track␈α
of␈α
variable␈α
bindings.

␈↓Let's␈α
sketch␈α
the␈α
evaluation␈α
of␈α
␈↓αfact[3]␈↓␈α
where:

␈↓␈↓ ∧=␈↓αfact <= λ[[x][x = 0 → 1;T → *[x;fact[x-1]]]].␈↓ 

␈↓That␈α
is,␈α
␈↓αeval[(FACT␈α
3);st]␈α
␈↓where␈α
␈↓αst␈↓␈α
names␈α
the␈α
initial␈α
symbol␈α
table,

␈↓␈↓ α;␈↓α((FACT LAMBDA(X)(COND((ZEROP X)1)(T(TIMES X(FACT (SUB1 X))))))).␈↓ 
␈↓␈↓↓54  Evaluation␈↓ 14.8␈↓


␈↓In␈α∨this␈α∨example␈α≡we␈α∨will␈α∨assume␈α≡that␈α∨the␈α∨binary␈α≡function,␈α∨*,␈α∨the␈α∨unary␈α≡predicate,
␈↓␈↓αzerop <= λ[[x]x = 0]␈↓␈α
and␈α
unary␈α
function,␈α
␈↓α␈α∞sub1 <= λ[[x] x-1]␈↓␈α
are␈α
known␈α
and␈α
are␈α
recognized␈α∞in␈α
the
␈↓evaluator␈α
by␈α
␈↓αTIMES,␈α
ZEROP␈↓␈α
and␈α
␈↓αSUB1␈↓␈α
respectively.

␈↓Then␈↓α eval[(FACT 3);st]
␈↓α␈↓ αX= apply[FACT;evlis[(3);st];st]
␈↓α␈↓ αX= apply[(LAMBDA(X)(COND ...));3;st]
␈↓α␈↓ αX= eval[(COND((ZEROP X)1)(T( ...)));((X . 3) . st)]
␈↓α␈↓ αX= evcond[(((ZEROP X)1)(T(TIMES X(FACT (SUB1 X)))));((X . 3) . st)]
␈↓α␈↓(Now, let ␈↓αst1␈↓ be␈↓α ((X . 3) . st) )
␈↓α␈↓ αX= eval[(TIMES X(FACT (SUB1 X))); st1]
␈↓α␈↓ αX= apply[TIMES;evlis[(X (FACT (SUB1 X))); st1];st1]
␈↓α␈↓ αX= apply[TIMES;cons[3;evlis[((FACT (SUB1 X))); st1];st1]

␈↓α␈↓Now things get a little interesting inside ␈↓αevlis:
␈↓α␈↓ αXevlis[(((FACT (SUB1 X)));st1]
␈↓α␈↓ αX␈↓ βx= cons[eval[(FACT (SUB1 X)); st1];NIL]
␈↓α␈↓ αX␈↓ βx␈↓ and ␈↓α eval[(FACT (SUB1 X));st1]
␈↓α␈↓ αX␈↓ βx␈↓ ¬_= apply[FACT;evlis[((SUB1 X));st1];st1]
␈↓α␈↓ αX␈↓ βx␈↓ ¬_= apply[FACT; (2);st1]
␈↓α␈↓ αX␈↓ βx␈↓ ¬_= apply[(LAMBDA (X)(COND ...));(2);st1]
␈↓α␈↓ αX␈↓ βx␈↓ ¬_= eval[(COND((ZEROP X) 1) ...));((X . 2) . st1)]
␈↓α␈↓ αX␈↓ βx␈↓ ¬_...

␈↓Notice␈α∞that␈α
within␈α∞this␈α∞latest␈α
call␈α∞on␈α∞␈↓αeval␈↓␈α
the␈α∞symbol-table-searching␈α∞function,␈α
␈↓αassoc␈↓,␈α∞will␈α∞find␈α
the
␈↓pair␈α␈↓α(X␈α
.␈α2)␈↓␈α
when␈αlooking␈α
for␈αthe␈α
value␈αof␈α
␈↓αx␈↓.␈αThis␈α
is␈αas␈α
it␈αshould␈α
be.␈α But␈α
notice␈αalso␈α
that␈αthe␈α
older
␈↓binding,␈α
␈↓α(X␈α.␈α
3)␈↓,␈α
is␈αstill␈α
around␈αin␈α
the␈α
symbol␈αtable␈α
␈↓αst1␈↓,␈αand␈α
will␈α
become␈αaccessible␈α
once␈αwe␈α
complete
␈↓this␈α
latest␈α
call␈α
on␈α
␈↓αeval␈↓.

␈↓As␈α
the␈α
computation␈α
continues,␈α
the␈α
symbol␈α
table␈α
is␈α
proceeding␈α
initially␈α
from,
␈↓α␈↓ ∧@st =((FACT LAMBDA(X)(COND ...))) ␈↓to␈↓α,
␈↓α␈↓ ¬l((X . 3) . st) ␈↓to␈↓α,
␈↓α␈↓ ¬@((X . 2)(X . 3) . st) ␈↓to␈↓α,
␈↓α␈↓ ∧i((X . 1)(X . 2)(X . 3) . st) ␈↓to finally␈↓α,
␈↓α␈↓ ∧|((X . 0)(X . 1)(X . 2)(X . 3) . st).

␈↓Since␈αthe␈αsearch␈αfunction,␈α␈↓αassoc␈↓,␈αalways␈αproceeds␈αfrom␈αleft␈αto␈αright␈αthrough␈αthe␈αtable␈αand␈αsince␈αthe
␈↓table␈αentry␈α
function,␈α␈↓αpairlis␈↓,␈α
always␈α␈↓αcons␈↓es␈α
pairs␈αonto␈αthe␈α
front␈αof␈α
the␈αtable␈α
before␈αcalling␈α
␈↓αeval␈↓,␈αwe
␈↓will␈α
get␈α
the␈α
correct␈α
binding␈α
of␈α
the␈α
variables.

␈↓This␈α∂symbol␈α⊂table␈α∂mechanism␈α⊂is␈α∂very␈α⊂important,␈α∂so␈α∂let's␈α⊂look␈α∂at␈α⊂it␈α∂again␈α⊂in␈α∂a␈α⊂slightly␈α∂different
␈↓manner.

␈↓We␈α
will␈α
represent␈α
calls␈α
on␈α
␈↓αeval␈↓␈α
informally,␈α
using␈α
LISP␈α
expressions␈α
rather␈α
than␈α
Sexprs␈α
as␈αinput.␈α
 We
␈↓represent␈α
the␈α
symbol␈α
table␈α
between␈α
vertical␈α
bars,␈α
"|",␈α
in␈α
such␈α
a␈α
way␈α
that␈α
if␈α
a␈α
table,␈α
t␈↓β1␈↓,␈α
is:
␈↓␈↓↓4.8␈↓ 	!Examples of ␈↓αeval␈↓↓     55␈↓α


␈↓␈↓ αX|  b␈↓βn␈↓␈↓ βH|
␈↓␈↓ αX|   ...␈↓ βH| then ␈↓αcons␈↓ing a new element, b␈↓βn+1␈↓ onto t␈↓β1␈↓ gives:
␈↓␈↓ αX|  b␈↓β1␈↓␈↓ βH|


␈↓␈↓ αX|  b␈↓βn+1␈↓␈↓ βH|
␈↓␈↓ αX|  b␈↓βn␈↓␈↓ βH|
␈↓␈↓ αX|   ...␈↓ βH|
␈↓␈↓ αX|  b␈↓β1␈↓␈↓ βH|

␈↓α␈↓Then:␈↓α

␈↓αeval[`fact[3]'; |fact = λ[[x][x=0 → 1;T → *[x;fact[x-1]]]]|]

␈↓α␈↓ αX= eval[`λ[[x][x=0 → 1; T → *[x;fact[x-1]]]];␈↓ π_|x = 3         | ]
␈↓α␈↓ αX␈↓ π_|fact = λ[[ ...  |

␈↓α␈↓ αX= *[3;eval[`λ[[x] ....';␈↓ π_|    x = 2      |]
␈↓α␈↓ αX␈↓ π_|    x = 3      |
␈↓α␈↓ αX␈↓ π_|fact = λ[[ ...] |


␈↓α␈↓ αX= *[3; *[2;eval[`λ[[x] ... ';␈↓ π_|    x = 1     |]
␈↓α␈↓ αX␈↓ π_|    x = 2     |
␈↓α␈↓ αX␈↓ π_|    x = 3     |
␈↓α␈↓ αX␈↓ π_|fact = λ[[ ... |


␈↓α␈↓ αX= *[3; *[2; *[1;eval[`λ[[x] ... ';␈↓ π_|    x = 0     |]
␈↓α␈↓ αX␈↓ π_|    x = 1     |
␈↓α␈↓ αX␈↓ π_|    x = 2     |
␈↓α␈↓ αX␈↓ π_|    x = 3     |
␈↓α␈↓ αX␈↓ π_|fact = λ[[ ... |

␈↓α␈↓ αX= *[3; *[2; *[1;1]]]  ␈↓with:␈↓α␈↓ π_|     x = 1     |
␈↓α␈↓ αX␈↓ π_|     x = 2    |
␈↓α␈↓ αX␈↓ π_|      ...         |

␈↓α␈↓ αX= *[3; *[2;1]]         ␈↓with:␈↓α␈↓ π_|     x = 2     |
␈↓α␈↓ αX␈↓ π_|      ...         |
␈↓␈↓↓56  Evaluation␈↓ 14.8␈↓


␈↓α␈↓ αX= *[3;2]               ␈↓with:␈↓α␈↓ π_|     x = 3     |
␈↓α␈↓ αX␈↓ π_|     ...          |

␈↓α␈↓ αX= 6.                   ␈↓with:␈↓α␈↓ π_|fact = λ[[ ... |.

␈↓α= 6

␈↓Notice␈αthat␈αafter␈αwe␈αwent␈αto␈αall␈αthe␈αtrouble␈αto␈αsave␈αthe␈αold␈αvalues␈αof␈α␈↓αx␈↓␈αwe␈αnever␈αhad␈αto␈αuse␈αthem.
␈↓However,␈α
in␈αthe␈α
general␈α
case␈αof␈α
recursive␈α
evaluation␈αwe␈α
must␈α
be␈αable␈α
to␈α
save␈αand␈α
restore␈α
the␈αold
␈↓values␈α
of␈α
variables.

␈↓For␈α
example␈α
recall␈α
the␈α
definition␈α
of␈α
␈↓αequal:


␈↓αequal <= λ[[x;y][atom[x] → [atom[y] → eq[x;y];T → NIL];
␈↓α␈↓ βH atom[y] → NIL;
␈↓α␈↓ βH equal[car[x];car[y]] → equal[cdr[x];cdr[y]];
␈↓α␈↓ βH T → NIL]]

␈↓α␈↓If we were evaluating:␈↓α
␈↓α␈↓ ¬equal[((A . B) . C);((A . B) . D)],
␈↓then our symbol table structure would be changing as follows:

␈↓α␈↓ αX|equal = λ[[x;y] ...| ==>␈↓ ε8|x = ((A . B) . C)| ==>
␈↓α␈↓ αX␈↓ ε8|y = ((A . B) . D)|
␈↓α␈↓ αX␈↓ ε8|equal = λ[[x;y]... |

␈↓α␈↓ αX|   x = (A . B)     |␈↓ ε8|       x = A        |
␈↓α␈↓ αX|   y = (A . B)     |␈↓ ε8|       y = A        |
␈↓α␈↓ αX| x = ((A . B). C)|   ==>␈↓ ε8|    x = (A . B)    |
␈↓α␈↓ αX| y = ((A . B). D)|␈↓ ε8|    y = (A . B)    | ==>
␈↓α␈↓ αX|equal = λ[[x;y ... |␈↓ ε8| x = ((A . B). C) |
␈↓α␈↓ αX␈↓ ε8| y = ((A . B). D) |
␈↓α␈↓ αX␈↓ ε8|equal = λ[[x;y] ...|

␈↓α␈↓ αX|       x = B         |␈↓ ε8|        x = C        |
␈↓α␈↓ αX|       y = B         |␈↓ ε8|        y = D        |
␈↓α␈↓ αX|    x = (A . B)     |␈↓ ε8|  x = ((A . B). C)  | ==>
␈↓α␈↓ αX|    y = (A . B)     | ==>␈↓ ε8|  y = ((A . B). D)  |
␈↓α␈↓ αX| x = ((A . B). C)  |␈↓ ε8|equal = λ[[x;y] ...  |
␈↓α␈↓ αX| y = ((A . B). D)  |
␈↓α␈↓ αX|equal = λ[[x;y] ... |


␈↓α␈↓ ¬X|equal = λ[[x;y] ... |
␈↓␈↓↓4.8␈↓ 	!Examples of ␈↓αeval␈↓↓     57␈↓α


␈↓This␈α
complexity␈α
is␈α
necessary,␈αfor␈α
while␈α
we␈α
are␈α
evaluating␈α␈↓αequal[car[x];car[y]]␈↓,␈α
we␈α
rebind␈α
␈↓αx␈↓␈α
and␈α␈↓αy␈↓␈α
but
␈↓we␈α
must␈α
save␈α
the␈α
old␈α
values␈α
of␈α
␈↓αx␈↓␈α
and␈α
␈↓αy␈↓␈α
for␈α
the␈α
possible␈α
evaluation␈α
of␈α
␈↓αequal[cdr[x];cdr[y]].␈↓

␈↓But␈αthe␈αclaim␈αis␈αthat␈αusing␈α
␈↓αpairlis␈↓␈αto␈αcons␈αthe␈αnew␈αbindings␈α
onto␈αthe␈αfront␈αof␈αthe␈αsymbol␈α
table␈αas
␈↓we␈α
call␈α
␈↓αeval␈↓␈α
does␈α
the␈α
right␈α
thing.␈α
That␈α
is,␈α
in␈α
␈↓αapply:

␈↓α␈↓ βSeq[car[fn];LAMBDA] → eval[caddr[fn];pairlis[cadr[fn];x;a]. 

␈↓α␈↓The␈α
tricky␈αpart␈α
is␈αthat␈α
when␈α
we␈αleave␈α
that␈αparticular␈α
call␈α
on␈α␈↓αapply␈↓,␈α
the␈αold␈α
table␈α
is␈αautomatically
␈↓restored␈αby␈αthe␈αrecursion␈α
mechanism.␈α That␈αis,␈αif␈α
␈↓αst␈↓␈αis␈αthe␈αcurrent␈α
symbol␈αtable␈αthen␈α␈↓αcons␈↓-ing␈α
things
␈↓onto␈α
the␈α
front␈α
of␈α
␈↓αst␈↓␈α
doesn't␈α
change␈α
␈↓αst␈↓,␈α
but␈α
if␈α
we␈α
call␈α
␈↓αeval␈↓␈α
or␈α
␈↓αapply␈↓␈α
with␈α
a␈α
symbol␈α
table␈α
of␈α
say:

␈↓␈↓ ¬⊃␈↓αcons[(X . 2);cons[(X . 3); st]] ␈↓ 

␈↓then␈α
in␈α
␈↓↓that␈↓␈α
call␈α
on␈α
␈↓αeval␈↓␈α
or␈α
␈↓αapply␈↓␈α
we␈α
have␈α
access␈α
to␈α
␈↓αx␈α
=␈α
2␈↓,␈α
not␈α
␈↓αx␈α
=␈α
3␈↓.



␈↓␈↓ ¬U␈↓↓4.9  Environments␈↓


␈↓This␈α
section␈α
will␈α
introduce␈α
one␈α
more␈αnotation␈α
for␈α
describing␈α
symbol␈α
tables␈α
or␈α
environments.␈αThis
␈↓notation,␈α_due␈α→to␈α_J.␈α→Weizenbaum,␈α_only␈α→shows␈α_the␈α→abstract␈α_structure␈α→of␈α_the␈α→symbol␈α_table
␈↓manipulations␈α∂during␈α∞evaluation.␈α∂Its␈α∞simplicity␈α∂will␈α∞be␈α∂of␈α∞great␈α∂benefit␈α∞when␈α∂we␈α∂introduce␈α∞more
␈↓complex␈α⊃binding␈α∩schemes␈α⊃necessary␈α⊃for␈α∩function-valued␈α⊃functions.␈α⊃See␈α∩Section␈α⊃4.12.␈α∩ The␈α⊃later
␈↓work␈α∞of␈α∞Bobrow␈α∞and␈α∞Wegbreit␈α∞covers␈α∞much␈α∞of␈α∞this␈α∞topic␈α∞but␈α∞in␈α∞more␈α∞detail␈α∞than␈α∞is␈α∂needed␈α∞for
␈↓clear␈α
understanding.

␈↓An␈α
environment␈α
will␈α
be␈α
described␈α
as␈α
:
␈↓␈↓ ¬x␈↓ ε8Form
␈↓␈↓ ¬x␈↓ ε8E␈↓βi␈↓
␈↓␈↓ ¬x␈↓ ε8|
␈↓␈↓ ¬x  E␈↓βc␈↓␈↓ ε8| E␈↓βa␈↓
␈↓␈↓ ¬x_________
␈↓␈↓ ¬xvar␈↓ ε8| value
␈↓␈↓ ¬x␈↓ ε8|
␈↓␈↓ ¬x    .....
␈↓␈↓ ¬x␈↓ ε8|


␈↓Form␈α
is␈α
the␈α
current␈α
form␈α∞being␈α
evaluated.␈α
 E␈↓βi␈↓␈α
is␈α
the␈α∞name␈α
of␈α
the␈α
current␈α
environment␈α∞or␈α
symbol
␈↓table.␈α
The␈α
evaluation␈α
of␈α
the␈α
current␈α
form␈α
takes␈α
place␈α
with␈α
respect␈α
to␈α
this␈α
environment.␈αVariables
␈↓appearing␈α
in␈αthe␈α
form␈αare␈α
evaluated␈αin␈α
this␈αenvironment.␈α
If␈α
the␈αvariable␈α
is␈αnot␈α
found␈αin␈α
E␈↓βi␈↓␈αthen␈α
E␈↓βa␈↓
␈↓is␈αthen␈αsearched.␈αIf␈αthe␈αvariable␈αis␈αnot␈αfound␈αin␈αE␈↓βa␈↓␈αthen␈αthe␈αenvironment␈αmentioned␈αin␈αthe␈αupper
␈↓right-hand␈αquadrant␈αof␈αE␈↓βa␈↓␈αis␈αsearched.␈αThe␈αsearch␈αwill␈αterminate␈αif␈αthe␈αvariable␈αis␈αfound␈αor␈αif␈αthe
␈↓␈↓↓58  Evaluation␈↓ 24.9␈↓


␈↓symbol␈α"/"␈αappears␈αin␈αthe␈αright-hand␈αquadrant.␈αIn␈αthis␈αsecond␈αcase␈αthe␈αvariable␈αis␈αundefined.␈αE␈↓βa␈↓␈αis
␈↓the␈α
beginning␈α
of␈α
the␈α
␈↓↓access␈α
evnironment␈↓␈α
for␈α
global␈α
variables.

␈↓E␈↓βc␈↓␈α
is␈α
called␈α
the␈α
␈↓↓control␈αenvironment␈↓.␈α
This␈α
environment␈α
is␈α
the␈αsymbol␈α
table␈α
to␈α
be␈α
restored␈αwhen␈α
the
␈↓evaluation␈α
on␈α
the␈α
current␈α
form␈α
has␈α
been␈α
completed.␈α
 In␈α
all␈α
examples␈α
we␈α
have␈α
seen␈α
so␈α
far␈α
E␈↓βa␈↓␈α
has
␈↓been␈α
the␈α
same␈α
as␈α
E␈↓βc␈↓.␈α
Things␈α
will␈α
soon␈α
get␈α
more␈α
complex.

␈↓For␈αnow,␈αthe␈αnotation␈αis␈αused␈αas␈αfollows:␈αwhen␈αwe␈αbegin,␈αan␈αinitial␈αtable␈αE␈↓β0␈↓␈αis␈αset␈αup␈αwith␈α"/"␈αin␈αits
␈↓access␈α∪and␈α∪control␈α∪fields.␈α∪Execution␈α∀of␈α∪"<="␈α∪will␈α∪add␈α∪a␈α∀var-val␈α∪entry␈α∪to␈α∪the␈α∪table.␈α∀When␈α∪a
␈↓λ-expression␈αis␈αentered,␈αi.e.,␈αwhen␈αwe␈αbind␈αthe␈αevaluated␈αarguments␈αto␈αthe␈αλ-variables,␈αa␈αnew␈α
E␈↓βi␈↓␈αis
␈↓set␈αup␈αwith␈αaccess␈αand␈α
control␈αbeing␈αthe␈αcurrent␈αenvironment.␈α
 Entries␈αreflecting␈αthe␈αbinding␈αof␈α
the
␈↓λ-variables␈α∩are␈α∪made␈α∩in␈α∪E␈↓βi␈↓␈α∩and␈α∪evaluation␈α∩of␈α∪the␈α∩λ-body␈α∪is␈α∩begun.␈α∪When␈α∩the␈α∪evalaution␈α∩is
␈↓completed␈α
old␈α
control␈α
environment␈α
is␈α
restored␈α
as␈α
the␈α
current␈α
environment.

␈↓First,␈α
consider␈α
the␈α
evaluation␈α
of␈α
␈↓αf[2;3]␈↓␈α
where␈α
␈↓αf <= λ[[x;y]x␈↓π2␈↓α + y]␈↓.␈α
 The␈α
flow␈α
of␈α
symbol␈α
tables␈α
is:

␈↓␈↓ αX␈↓ βλ␈↓αf <= ...␈↓ βX␈↓ ∧(␈↓ ∧Xf[2;3]␈↓ εH␈↓ π_␈↓ πHx␈↓π2␈↓α + y␈↓
␈↓␈↓ αX␈↓ βλE␈↓β0␈↓␈↓ βX␈↓ ∧(␈↓ ∧XE␈↓β0␈↓␈↓ εH␈↓ π_␈↓ πHE␈↓β1␈↓␈↓ λ_␈↓ λH␈↓ λxE␈↓β0␈↓
␈↓␈↓ αX /␈↓ βλ| /␈↓ βX␈↓ ∧( /␈↓ ∧X| /␈↓ εH␈↓ π_E␈↓β0␈↓␈↓ πH| E␈↓β0␈↓␈↓ λ_␈↓ λH /␈↓ λx| /
␈↓␈↓ αX______␈↓ βX=>␈↓ ∧(______␈↓ εH=>␈↓ π_______␈↓ λ_=>␈↓ λH______
␈↓␈↓ αX␈↓ βλ|␈↓ βX␈↓ ∧( ␈↓αf␈↓ ∧X| λ[[x;y]x␈↓π2␈↓α + y]␈↓␈↓ εH␈↓ π_␈↓αx␈↓ πH| 2␈↓ λ_␈↓ λHf␈↓ λx| λ[[x;y] ...
␈↓α␈↓ αX␈↓ βλ␈↓ βX␈↓ ∧(␈↓ ∧X␈↓ εH␈↓ π_ y␈↓ πH|3␈↓


␈↓The␈α
execution␈α
of␈α
␈↓αfact[3]␈↓␈α
on␈α
page␈α
53␈α
results␈α
in␈α
a␈α
more␈α
interesting␈α
example.
␈↓␈↓ αX␈↓ βλ␈↓αfact[3]␈↓ βX␈↓ ∧(␈↓ ∧Xfact[2]␈↓ ¬(␈↓ ¬x␈↓ ε(fact[1]␈↓ εx␈↓ π(␈↓ πXfact[0]␈↓
␈↓␈↓ αX␈↓ βλE␈↓β0␈↓␈↓ βX␈↓ ∧(␈↓ ∧XE␈↓β1␈↓␈↓ ¬(␈↓ ¬x␈↓ ε(E␈↓β2␈↓␈↓ εx␈↓ π(␈↓ πXE␈↓β3␈↓␈↓ λ(␈↓ λx␈↓ 	(E␈↓β4␈↓   ...etc.
␈↓␈↓ αX /␈↓ βλ| /␈↓ βX␈↓ ∧(E␈↓β0␈↓␈↓ ∧X|  E␈↓β0␈↓␈↓ ¬(␈↓ ¬xE␈↓β1␈↓␈↓ ε(|  E␈↓β1␈↓␈↓ εx␈↓ π(E␈↓β2␈↓␈↓ πX|  E␈↓β2␈↓␈↓ λ(␈↓ λxE␈↓β3␈↓␈↓ 	(|  E␈↓β3␈↓  ...
␈↓␈↓ αX______␈↓ βX=>␈↓ ∧(______␈↓ ¬(=>␈↓ ¬x______␈↓ εx=>␈↓ π(______␈↓ λ(=>␈↓ λx______
␈↓␈↓ αX␈↓αfact␈↓ βλ| λ[[x] ...␈↓ ∧( x␈↓ ∧X| 3␈↓ ¬(␈↓ ¬x x␈↓ ε(| 2␈↓ εx␈↓ π( x␈↓ πX| 1␈↓ λ(␈↓ λx x␈↓ 	(| 0   ...␈↓

␈↓Notice in this example we will get the correct binding of ␈↓αx␈↓.

␈↓As␈α
a␈α
final␈α
example␈α
using␈α
non-local␈α
variable␈α
bindings␈α
consider:

␈↓␈↓αf[3]␈↓␈α
where:␈α
␈↓α␈α
f␈α
<=␈α
λ[[x]g[2]]␈↓␈α
and␈α
␈↓αg␈α
<=␈α
λ[[y]␈α
x+y]␈↓.
␈↓␈↓ αX␈↓αf <= ...; g <= ...␈↓ ∧(f[3]␈↓ ∧X␈↓ εH␈↓ π_g[2]␈↓ πH␈↓ λ_␈↓ λHx + y     ....
␈↓α␈↓ αX␈↓ βλE␈↓β0␈↓α␈↓ βX␈↓ ∧(␈↓ ∧XE␈↓β0␈↓α␈↓ εH␈↓ π_␈↓ πHE␈↓β1␈↓α␈↓ λ_␈↓ λH␈↓ λxE␈↓β2␈↓α             ....
␈↓α␈↓ αX /␈↓ βλ| /␈↓ βX␈↓ ∧( /␈↓ ∧X| /␈↓ εH␈↓ π_E␈↓β0␈↓α␈↓ πH| E␈↓β0␈↓α␈↓ λ_␈↓ λH E␈↓β1␈↓α␈↓ λx| E␈↓β1␈↓α
␈↓α␈↓ αX______␈↓ βX=>␈↓ ∧(______␈↓ εH=>␈↓ π_______␈↓ λ_=>␈↓ λH______␈↓ 	H           ......
␈↓α␈↓ αX␈↓ βλ|␈↓ βX␈↓ ∧( f␈↓ ∧X| λ[[x] g[x]]␈↓ εH␈↓ π_x␈↓ πH| 3␈↓ λ_␈↓ λHy␈↓ λx| 2       ...
␈↓α␈↓ αX␈↓ βλ␈↓ βX␈↓ ∧(g␈↓ ∧X| λ[[y] x+y]

␈↓Notice␈αthat␈αwhen␈αwe␈αattempt␈αto␈αevaluate␈α␈↓αx␈α+␈αy␈↓␈αwe␈αfind␈α␈↓αy␈↓␈αhas␈αa␈αlocal␈αvalue,␈αbut␈αwe␈αmust␈αlook␈αdown
␈↓the␈α
access␈α
chain␈α
to␈α
find␈α
a␈α
binding␈α
for␈α
␈↓αx␈↓.
␈↓␈↓↓4.10␈↓ πhHacking with ␈↓αeval␈↓↓ and friends.     59␈↓α


␈↓␈↓ ∧]␈↓↓4.10  Hacking with ␈↓αeval␈↓↓ and friends.␈↓α


␈↓****MORE PROBBS***

␈↓Assume that the variable, ␈↓αst␈↓, is currently bound to:
␈↓␈↓ ∧m␈↓α((X . M)(Y . T)(Z .(M N))(T . T)).

␈↓evaluate:

␈↓␈↓↓1.␈↓α assoc[Z;st]
␈↓α␈↓↓2.␈↓α eval[(QUOTE A);st]
␈↓α␈↓↓3.␈↓α apply[CONS;(A B); st]
␈↓α␈↓↓4.␈↓α apply[CAR;((A));st]
␈↓α␈↓↓5.␈↓α apply[CAR;(A);st]


␈↓α␈↓II␈α
Write␈α
a␈α
version␈α
of␈α
the␈α
LISP␈α
evaluator␈α
which␈α
does␈α
call-by-name␈α
rather␈α
than␈α
call-by-value.



␈↓␈↓ ε⊃␈↓↓4.11  ␈↓αlabel␈↓↓␈↓α


␈↓One␈α⊃effect␈α⊃of␈α⊂placing␈α⊃"λ"␈α⊃and␈α⊃a␈α⊂list␈α⊃of␈α⊃λ-variables␈α⊂in␈α⊃front␈α⊃of␈α⊃an␈α⊂expresson␈α⊃is␈α⊃to␈α⊃bind␈α⊂those
␈↓variables␈αwhich␈αappear␈αboth␈αin␈αthe␈αλ-list␈αand␈αthe␈αexpression.␈αAll␈αother␈αvariables␈αappearing␈αin␈αthe
␈↓expression␈α
are␈α
␈↓↓free␈α
variables␈↓.␈α
 For␈α
example,␈α
␈↓αf␈↓␈α
is␈α
free␈α
in␈α
the␈α
following:

␈↓␈↓ ∧G␈↓αf <= λ[[x][zerop[x] → 1; T → *[x;f[x-1]]] ].␈↓ 

␈↓Clearly␈α
our␈αintention␈α
is␈α
that␈αthe␈α
␈↓αf␈↓␈α
appearing␈αthe␈α
the␈αright␈α
of␈α
"<="␈αis␈α
the␈α
same␈α␈↓αf␈↓␈α
appearing␈α
to␈αthe
␈↓left␈α
of␈α
"<=".␈α
That␈α
is␈α
we␈α
want␈α
␈↓αf␈↓␈α
to␈α
be␈α
a␈α
bound␈α
variable␈α
and␈α
not␈α
free.

␈↓This␈αhas␈αnot␈αbeen␈αa␈αproblem␈αfor␈αus.␈α We␈αhave␈αsimply␈αpre-loaded␈αthe␈αsymbol␈αtable,␈αbinding␈α␈↓αf␈↓␈αto␈αits
␈↓definition␈α(or␈αvalue).␈αSee␈αpage␈α53.␈αLISP␈αhas␈αbeen␈αequipped␈αwith␈αa␈αslightly␈αmore␈αelegant␈αdevice␈αfor
␈↓this␈α
binding.␈α
 It␈α
is␈α
called␈α
the␈α
␈↓αlabel␈↓␈α
operator.␈α
It␈α
is␈α
called␈α
thus:

␈↓␈↓ ¬∃␈↓αlabel[␈↓<identifier>;<function>]

␈↓and␈α
has␈α
exactly␈α
the␈α
effect␈α
of␈α
binding␈α
the␈α
<identifier>␈α
to␈α
the␈α
<function>.

␈↓Note␈α
that␈α
␈↓αlabel␈↓␈α
is␈α
a␈α
special␈α
form.␈α
To␈α
include␈α
␈↓αlabel␈↓␈α
in␈α
the␈α
LISP␈α
syntax␈α
add:

␈↓<function>␈↓ αh::= ␈↓αlabel␈↓[<identifier>;<function>]

␈↓and the sexpr translation of the ␈↓αlabel␈↓ construct should naturally be:
␈↓␈↓↓60  Evaluation␈↓ (4.11␈↓


␈↓␈↓ βE␈↓α(LABEL ␈↓translate of <identifier>    translate of <function> ␈↓α).


␈↓␈↓↓␈↓ ε↔Problem␈↓

␈↓1.␈α
 Show␈α
how␈α
to␈α
change␈α
␈↓αeval␈↓␈α
to␈α
handle␈α
␈↓αlabel␈↓.



␈↓␈↓ ¬~␈↓↓4.12  functional arguments␈↓


␈↓Recall␈α⊃our␈α⊃discussion␈α⊃of␈α⊃:␈α⊃␈↓αeval[(F 2 3);((F .(LAMBDA(X Y)(PLUS (EXPT X 2)Y))))].␈↓␈α⊃We␈α⊃now
␈↓know␈α∞this␈α
is␈α∞equivalent␈α∞to:␈α
␈↓αeval[((LABEL F (LAMBDA(X Y)(PLUS (EXPT X 2) Y))) 2 3);NIL]␈↓.
␈↓In␈α
either␈α
case,␈α
the␈αeffect␈α
is␈α
to␈α
bind␈α
the␈αname␈α
␈↓αf␈↓␈α
to␈α
the␈α
λ-expression.␈α Binding␈α
is␈α
also␈α
going␈α
on␈αwhen␈α
␈↓αf␈↓
␈↓is␈α∂called:␈α⊂we␈α∂bind␈α⊂␈↓α2␈↓␈α∂to␈α⊂␈↓αx␈↓,␈α∂and␈α⊂␈↓α3␈↓␈α∂to␈α∂␈↓αy␈↓.␈α⊂Acknowledging␈α∂Occam,␈α⊂why␈α∂not␈α⊂attempt␈α∂to␈α⊂combine␈α∂the
␈↓binding␈α
processes?␈α
 For␈α
example␈α
if␈α␈↓α␈α
twice[x]␈↓␈α
is␈α
defined␈α
to␈α
be␈α␈↓αplus[x;x]␈↓,␈α
and␈α
␈↓αfoo[f;x]␈↓␈α
were␈α
␈↓αf[x]␈↓,␈αthen␈α
a
␈↓truly␈α
expensive␈α
way␈α
of␈α
evaluating␈α
␈↓αtwice[2]␈↓␈α
would␈α
appear␈α
to␈α
be␈α
␈↓αfoo[twice;2]␈↓.

␈↓This␈α∞will␈α∞almost␈α∂work;␈α∞as␈α∞usual␈α∞␈↓αfoo␈↓␈α∂would␈α∞be␈α∞considered␈α∞a␈α∂␈↓↓function␈↓␈α∞by␈α∞␈↓αeval␈↓␈α∞and␈α∂the␈α∞arguments
␈↓would␈αbe␈α
evaluated.␈α We␈α
don't␈αwant␈αthe␈α
␈↓↓value␈↓␈αof␈α
␈↓αtwice␈↓,␈αwe␈αwant␈α
the␈α␈↓↓name␈↓.␈α
So␈αto␈α
stop␈αevaluation,
␈↓we␈α
might␈α
try␈α
␈↓αQUOTE␈↓-ing␈α
␈↓αtwice␈↓␈α
when␈α
we␈α
call␈α
␈↓αfoo␈↓;␈α
thus␈α
␈↓α(FOO (QUOTE TWICE) 2)␈↓.␈α
␈↓αQUOTE␈↓-ing␈α
is
␈↓not␈αquite␈αgood␈αenough,␈αas␈αwe␈αwill␈αsee␈αin␈αa␈αmoment,␈αbut␈αit␈αwould␈αwork␈αhere.␈α␈↓αf␈↓␈αin␈αthe␈αdefinition␈αof
␈↓␈↓αfoo␈↓␈α∞is␈α∂called␈α∞a␈α∂␈↓↓functional␈α∞argument␈↓;␈α∂␈↓αf␈↓␈α∞is␈α∂a␈α∞λ-variable,␈α∂but␈α∞appears␈α∂in␈α∞the␈α∂body␈α∞of␈α∂the␈α∞function
␈↓where␈α∪we␈α∪expect␈α∩a␈α∪function.␈α∪You,␈α∩of␈α∪course,␈α∪should␈α∩realize␈α∪by␈α∪now␈α∩that␈α∪␈↓↓all␈↓␈α∪function␈α∩names
␈↓appearing␈α∃in␈α⊗our␈α∃LISP␈α⊗expressions␈α∃are␈α∃variables;␈α⊗hopefully␈α∃they␈α⊗are␈α∃bound␈α⊗to␈α∃␈↓↓something␈↓
␈↓(primitives␈α
or␈α
λ-expressions)␈α
before␈α
we␈α
attempt␈α
to␈α
evaluate␈α
them.

␈↓Using␈α
Weizenbaum's␈α
environments␈α
we␈α
might␈α
get:

␈↓␈↓ αX␈↓αfoo[twice;2]␈↓ ∧(␈↓ ∧xtwice[2]␈↓ π_␈↓ πh   x + x␈↓
␈↓␈↓ αX␈↓ βλE␈↓β0␈↓␈↓ ∧(␈↓ ∧x␈↓ ¬(E␈↓β1␈↓␈↓ π_␈↓ πh␈↓ λ_E␈↓β2␈↓
␈↓␈↓ αX /␈↓ βλ| /␈↓ ∧(␈↓ ∧x E␈↓β0␈↓␈↓ ¬(| E␈↓β0␈↓␈↓ π_␈↓ πh E␈↓β1␈↓␈↓ λ_| E␈↓β1␈↓
␈↓␈↓ αX______␈↓ ∧(=>␈↓ ∧x______␈↓ π_=>␈↓ πh______
␈↓␈↓α         twice␈↓ αX␈↓ βλ| λ[[x] x + x␈↓ ∧(␈↓ ∧x x␈↓ ¬(| f␈↓ π_␈↓ πh x␈↓ λ_| 2
␈↓α           foo␈↓ αX␈↓ βλ| λ[[x;f] f[x]]␈↓ ∧(␈↓ ∧x f␈↓ ¬(| "twice"

␈↓α␈↓So we ␈↓↓will␈↓ get the right bindings.

␈↓If␈α∪such␈α∩baroque␈α∪examples␈α∩were␈α∪the␈α∩limit␈α∪of␈α∩functional␈α∪arguments,␈α∩their␈α∪usefulness␈α∪would␈α∩be
␈↓questionable,␈αhowever␈αthey␈αare␈αboth␈αa␈αpowerful␈αprogramming␈αtool␈αand␈αtheir␈αimplementation␈αsheds
␈↓much␈α
light␈α
on␈α
programming␈α
language␈α
design␈α
(see␈α
Section␈α
7.12).

␈↓In␈α
particular,␈α
most␈α
implementations␈α
of␈α
LISP␈α
include␈α
a␈α
very␈α
useful␈α
class␈α
of␈α
mapping␈α
functions.
␈↓␈↓↓4.12␈↓ λcfunctional arguments     61␈↓


␈↓␈↓αmaplist␈↓␈αis␈αa␈αfunction␈αof␈αtwo␈αarguments,␈αa␈αlist,␈α␈↓αl␈↓,␈αand␈αa␈αfunctional␈αargument,␈α␈↓αfn␈↓.␈α␈↓αmaplist␈↓␈αapplies␈αthe
␈↓␈↓ αhfunction␈α␈↓αfn␈↓␈α(of␈αone␈αargument)␈αto␈αthe␈αlist␈α␈↓αl␈↓␈αand␈αits␈α␈↓αcdr␈↓s␈αuntil␈α␈↓αl␈↓␈αis␈αreduced␈αto␈α␈↓αNIL␈↓.␈α The
␈↓␈↓ αhvalue␈α
of␈α
␈↓αmaplist␈↓␈α
is␈α
the␈α
list␈α
of␈α
the␈α
values␈α
returned␈α
by␈α
␈↓αfn␈↓.␈α
 Here's␈α
a␈α
definition␈α
of␈α
␈↓αmaplist␈↓:

␈↓␈↓ β⊂␈↓αmaplist <= λ[[fn;l][null[l] → NIL; T → cons[fn[l];maplist[fn;cdr[l]]]]]␈↓. 

␈↓Thus:

␈↓␈↓α␈↓ β3maplist[REVERSE;(A B C D)] = ((D C B A)(D C B)(D C)(D))␈↓. 

␈↓␈↓αmapcar␈↓␈α
is␈α
a␈α
similar␈α
function,␈α
applying␈α
␈↓αfn␈↓␈α
not␈α
to␈α
␈↓αl␈↓␈α
but␈α
the␈α
␈↓αcar␈↓␈α
of␈α
␈↓αl␈↓.␈α
Thus:

␈↓␈↓ αu␈↓αmapcar <= λ[[fn;l][null[l] → NIL; T → cons[fn[car[l]];mapcar[fn;cdr[l]]]]].␈↓ 

␈↓and␈α
an␈α
example:

␈↓␈↓ β$␈↓αmapcar[(LAMBDA(X)(CONS X NIL));(A B C D)] = ((A)(B)(C)).␈↓ 

␈↓To␈α
see␈α
why␈α
␈↓αQUOTE␈↓␈α
is␈α
not␈α
sufficient,␈α
consider␈α
the␈α
following␈α
strange␈α
example:
␈↓␈↓α␈↓ αXtester <= λ[[l;fn]␈↓ ¬λ[null[l] → NIL;
␈↓α␈↓ αX␈↓ ¬λ atom[l] → fn[ ];
␈↓α␈↓ αX␈↓ ¬λ T → tester[car[l];(LAMBDA()(TESTER(CDR L) FN))]]]␈↓

␈↓Notice␈α
␈↓αfn␈↓␈α
is␈α
a␈αfunctional␈α
argument␈α
and␈α
we␈αare␈α
␈↓αQUOTE␈↓-ing␈α
the␈α
actual␈αparameter␈α
bound␈α
to␈α
␈↓αfn␈↓␈αin␈α
the
␈↓recursive␈α5call␈α5on␈α4the␈α5function,␈α5␈↓αtester␈↓.␈α5 Now␈α4if␈α5we␈α5call␈α5␈↓αtester␈↓␈α4with:
␈↓␈↓αtester[(A (B) (C));(LAMBDA()(BAZ (QUOTE A))]␈↓␈α
the␈α
following␈α
occurs:

␈↓␈↓↓1.␈↓␈α
␈↓αl␈↓␈α
is␈α
bound␈α
to␈α
␈↓α(A␈α
(B)␈α
(C))␈↓;␈α
␈↓αfn␈↓␈α
is␈α
bound␈α
to␈α
␈↓α(LAMBDA()(BAZ (QUOTE A)))␈↓.

␈↓␈↓↓2␈↓.␈α In␈αthe␈αconditional␈αexpression␈αof␈α␈↓αtester␈↓␈αp␈↓β1␈↓␈αand␈αp␈↓β2␈↓␈αare␈αfalse␈αand␈αthus␈αwe␈αevaluate␈αthe␈α"otherwise"
␈↓branch.

␈↓␈↓↓3␈↓.␈α
 We␈α
rebind␈α
␈↓αl␈↓␈α
to␈α
␈↓αA␈↓,␈α
rebind␈α
␈↓αfn␈↓␈α
to␈α
␈↓α(LAMBDA()(TESTER(CDR L) FN))␈↓␈α
and␈α
lose␈α
big.

␈↓␈↓αnull[l]␈↓␈α
is␈αfalse,␈α
but␈α␈↓αatom[l]␈↓␈α
is␈α
true␈αso␈α
we␈αevaluate␈α
␈↓αfn␈↓.␈α When␈α
we␈α
attempt␈αto␈α
evaluate␈α␈↓αcdr[l]␈↓␈α
we␈αget␈α
the
␈↓wrong␈α∞binding␈α∞of␈α∞␈↓αl␈↓.␈α∞ What␈α∞we␈α∞find␈α∞bound␈α∞to␈α∞␈↓αl␈↓␈α∞is␈α∞␈↓αA␈↓,␈α∞rather␈α∞than␈α∞␈↓α(A (B)(C))␈↓␈α∞which␈α∂we␈α∞expected.
␈↓The␈αenvironment␈αat␈αthe␈αpoint␈αof␈αcall␈αin␈αe␈↓β3␈↓␈αof␈αthe␈αbody␈αof␈α␈↓αtester␈↓␈αshould␈αhave␈αbeen␈αassociated␈αwith
␈↓␈↓αfn␈↓␈α∞when␈α∞we␈α∂rebound␈α∞␈↓αfn␈↓.␈α∞ That␈α∂is,␈α∞we␈α∞must␈α∞associate␈α∂the␈α∞symbol␈α∞table␈α∂which␈α∞was␈α∞current␈α∂at␈α∞the
␈↓␈↓↓point␈α
of␈α
call␈↓␈α
with␈α
the␈α
functional␈α
argument.

␈↓We␈α
must␈α
now␈α
modify␈αour␈α
use␈α
of␈α
the␈α
Weizenbaum␈αenvironments␈α
to␈α
show␈α
this␈α
association.␈α When␈α
an
␈↓assignment␈αof␈αthe␈αform␈α␈↓αf <= λ[[ ... ] ... ]␈↓␈αis␈αperformed␈αwe␈αwill␈αadd␈αthe␈αname␈α␈↓αf␈↓␈αas␈αusual␈αbut␈αwill␈αnow
␈↓add␈α∂␈↓αλ[[ ... ] ... ] :␈↓E␈↓βi␈↓␈α∂as␈α∂value␈α∂where␈α∞E␈↓βi␈↓␈α∂is␈α∂the␈α∂environment␈α∂at␈α∞the␈α∂point␈α∂of␈α∂assignment.␈α∂When␈α∂␈↓αf␈↓␈α∞is
␈↓␈↓↓called␈↓␈α
we␈α∞set␈α
up␈α
a␈α∞new␈α
environment␈α
as␈α∞before,␈α
but␈α
the␈α∞access␈α
environment␈α
will␈α∞now␈α
be␈α
set␈α∞to␈α
E␈↓βi␈↓.
␈↓This␈α∀solution␈α∃might␈α∀seem␈α∀a␈α∃bit␈α∀drastic,␈α∀but␈α∃it␈α∀is␈α∀easy␈α∃to␈α∀construct␈α∀counterexamples␈α∃to␈α∀less
␈↓␈↓↓62  Evaluation␈↓ &4.12␈↓


␈↓sophisticated␈α∞solutions.␈α∞ For␈α∞example,␈α∞attempting␈α∞to␈α∂substitute␈α∞value␈α∞for␈α∞the␈α∞free␈α∞variables␈α∂at␈α∞the
␈↓time␈α
"<="␈α
is␈α
done␈α
is␈α
not␈α
sufficient.␈α
 Consider␈α
the␈α
following␈α
example:
␈↓␈↓ αX␈↓ βλ␈↓αg[3]␈↓ ∧x␈↓ ¬H␈↓ ¬xf[2]␈↓ πh␈↓ λ8␈↓ λhx + y + z       ....␈↓
␈↓␈↓ αX␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓␈↓ πh␈↓ λ8␈↓ λhE␈↓β2␈↓     ...
␈↓␈↓ αX /␈↓ βλ| /␈↓ ∧x␈↓ ¬HE␈↓β0␈↓␈↓ ¬x| E␈↓β0␈↓␈↓ πh␈↓ λ8E␈↓β1␈↓␈↓ λh| E␈↓β0␈↓    ....
␈↓␈↓ αX______␈↓ ∧x=>␈↓ ¬H______␈↓ πh=>␈↓ λ8______    ....
␈↓␈↓ αX␈↓αf␈↓ βλ| λ[[x]x+y+z]:␈↓E␈↓β0␈↓α␈↓ ∧x␈↓ ¬Hy␈↓ ¬x| 3␈↓ πh␈↓ λ8x␈↓ λh| 2
␈↓α␈↓ αXg␈↓ βλ| λ[[y]f[2]]
␈↓α␈↓ αXz␈↓ βλ| 4

␈↓Notice␈α
that␈α
we␈α
could␈αsubstitute␈α
the␈α
value␈α
for␈α␈↓αz␈↓␈α
when␈α
␈↓αf␈↓␈α
is␈αdefined␈α
but␈α
we␈α
cannot␈αdo␈α
so␈α
for␈α
␈↓αy␈↓.␈αEven␈α
if
␈↓there␈α
were␈α
a␈α
value␈α
for␈α
␈↓αy␈↓␈α
at␈α
this␈α
time␈α
it␈α
would␈α
be␈α
the␈α
wrong␈α
value.

␈↓What␈α∂does␈α∂this␈α∂say␈α⊂about␈α∂functions?␈α∂ We␈α∂have␈α⊂already␈α∂remarked␈α∂that␈α∂functions␈α⊂are␈α∂parametric
␈↓values;␈αto␈αthat␈αwe␈αmust␈αadd:␈αfunctions␈αare␈αalso␈αtied␈αto␈αthe␈αenvironment␈αin␈αwhich␈αthey␈αwere␈αcreated;
␈↓they␈α
cannot␈α
be␈α
evaluated␈α
␈↓αin␈α
vacuo␈↓.

␈↓The␈α⊃device␈α⊃LISP␈α⊂used␈α⊃to␈α⊃associate␈α⊂environments␈α⊃with␈α⊃functions␈α⊂is␈α⊃called␈α⊃the␈α⊃␈↓αFUNARG␈↓␈α⊂hack.
␈↓(More␈αis␈αsaid␈αabout␈α␈↓αFUNARG␈↓␈αin␈αSection␈α
7.12.)␈αInstead␈αof␈αdesignating␈αan␈αinstance␈αof␈α
a␈αfunctional
␈↓argument␈α
by␈α
the␈α
␈↓αQUOTE␈↓␈α
operator,␈α
we␈α
introduce␈α
a␈α
new␈α
operator␈α
called␈α
␈↓αFUNCTION␈↓.␈α
Thus:

␈↓␈↓ ∧␈␈↓αfunction[λ[[]tester[cdr[l];fn]]] ␈↓or, 

␈↓␈↓ βn␈↓α(FUNCTION(LAMBDA()(TESTER (CDR L) FN))).␈↓ 

␈↓When␈α
␈↓αeval␈↓␈α
sees␈α
the␈α
construction␈α
␈↓α(FUNCTION␈α
␈↓fn␈↓α)␈↓␈α
it␈α
returns␈α
as␈α
value␈α
the␈α
list:

␈↓␈↓ ∧K␈↓α(FUNARG   ␈↓fn    current-symbol-table␈↓α)␈↓. 

␈↓When␈α
␈↓αeval␈↓,␈α∞or␈α
actually␈α∞␈↓αapply␈↓␈α
sees␈α∞␈↓α(FUNARG␈α
␈↓fn␈α∞st␈↓α)␈↓,␈α
that␈α
is,␈α∞when␈α
we␈α∞are␈α
␈↓↓calling␈↓α␈α∞fn␈↓,␈α
we␈α∞use␈α
the
␈↓symbol␈α
table␈α
␈↓αst␈↓,␈α
rather␈α
than␈α
the␈α
current␈α
symbol␈α
table␈α
for␈α
accessing␈α
global␈α
variables.

␈↓Let's␈α∪follow␈α∪the␈α∪behavior␈α∪of␈α∪an␈α∪␈↓αeval␈↓␈α∩and␈α∪␈↓αapply␈↓␈α∪family␈α∪which␈α∪has␈α∪been␈α∪modified␈α∪to␈α∩handle
␈↓functional␈α
arguments.

␈↓Let␈α
␈↓αfn␈α<=␈α
λ[[x␈↓β1␈↓α;␈α
...␈α;x␈↓βn␈↓α]␈α
...]␈↓␈α
be␈αa␈α
function␈α
which␈αwill␈α
be␈α
used␈αas␈α
a␈α
functional␈αargument␈α
in␈αthe␈α
context,
␈↓say:

␈↓α␈↓ ¬fie <= λ[[ ...;foo ...]  .... foo[ ...] ]
␈↓α␈↓ ¬>fie[ ...; function[fn]; ...]


␈↓in␈αthe␈αsequel␈α␈↓αst␈↓␈αand␈α␈↓αst␈↓βsave␈↓␈αwill␈αname␈αsymbol␈αtables.␈α"→"␈αwill␈αmean␈α"points␈αto"␈α(i.e., ␈↓αcons␈↓).␈αp␈↓βi␈↓'s␈αwill␈αbe
␈↓dotted␈α
pairs␈α
in␈α
a␈α
symbol␈α
table.␈α
 Then␈α
let's␈α
see␈α
what␈α
calling␈α
␈↓αfie[ ...; function[fn]; ...]␈↓␈α
will␈α
do.
␈↓␈↓↓4.12␈↓ λcfunctional arguments     63␈↓



␈↓␈↓ αX␈↓α(FIE ... (FUNCTION FN) ...)  st:␈↓ λxNIL    ␈↓π 5␈↓α
␈↓α␈↓ αX         ....
␈↓α␈↓ αX␈↓computation               ␈↓αst␈↓ :␈↓ λxp␈↓βn␈↓ → p␈↓βn-1␈↓ → ... p␈↓β1␈↓
␈↓␈↓ αX␈↓α(FUNCTION FN)
␈↓α␈↓ αX␈↓   gives:
␈↓␈↓ αX␈↓α(FUNARG FN st␈↓βsave␈↓α)
␈↓α␈↓ αX         ....
␈↓α␈↓ αX␈↓computation         ␈↓αst␈↓ : p␈↓βm␈↓ → p␈↓βm-1␈↓ ...→
␈↓␈↓ αX␈↓α(FOO ␈↓a␈↓β1␈↓ ... a␈↓βn␈↓)
␈↓␈↓ αX   gives:
␈↓␈↓ αX␈↓α((FUNARG FN st␈↓βsave␈↓α)␈↓a␈↓β1␈↓ ... a␈↓βn␈↓α)


␈↓α␈↓The␈αa␈↓βi␈↓'s␈α
are␈αevaluated␈α
in␈αthe␈α
context␈α␈↓αst␈↓␈α
␈↓↓not␈↓␈α␈↓αst␈↓βsave␈↓
␈↓by␈α
␈↓αevlis[ ...; st]␈↓␈α
giving␈α
v␈↓βi␈↓'s.

␈↓We␈α
then␈α
apply␈α
␈↓αfn␈↓␈α
to␈α
the␈α
v␈↓βi␈↓'s␈α
in␈α
the␈α
context␈α␈↓αst␈↓βsave␈↓
␈↓␈↓↓not␈↓␈α9in␈α9environment␈α9␈↓αst␈↓␈α:by␈α9calling
␈↓␈↓αapply[ ... ; ... ;st␈↓βsave␈↓α].

␈↓α␈↓This␈α
results␈α
in:

␈↓␈↓αeval[␈α
␈↓body␈α
of␈α
␈↓αfn␈↓;␈α
␈↓α((x␈↓β1␈↓␈α
.␈α
v␈↓β1␈↓α)␈α
→␈α
...␈α
(x␈↓βn␈↓␈α
.␈α
v␈↓βn␈↓α)␈↓␈α
→

␈↓After␈α
we␈α
finish␈α
this␈α
inner␈α
call␈α
on␈α
␈↓αapply[ ... ; ... ;st␈↓βsave␈↓α]␈↓␈α
the␈α
table␈α
␈↓αst␈↓␈α
is␈α
restored.␈α
Notice␈α
that␈α
our␈α
symbol
␈↓tables␈αare␈α
now␈αreally␈α
tree-like␈αrather␈α
than␈αstack-like.␈α
 It␈αshould␈α
be␈αapparent␈α
from␈α␈↓αeval␈↓␈αthat␈α
␈↓α(QUOTE
␈↓α...)␈↓␈α
and␈α
␈↓α(FUNCTION␈α
...)␈↓␈α
will␈α
have␈α
the␈α
same␈α
effect␈α
if␈α
␈↓αfn␈↓␈α
has␈α
no␈α
global␈α
(or␈α
free)␈α
variables.

␈↓This␈α∂seems␈α∞like␈α∂a␈α∞lot␈α∂of␈α∂work␈α∞to␈α∂allow␈α∞a␈α∂moderately␈α∂obscure␈α∞construct␈α∂to␈α∞appear␈α∂in␈α∂a␈α∞language.
␈↓However␈α∀constructs␈α∪like␈α∀functional␈α∪arguments␈α∀appear␈α∪in␈α∀most␈α∪programming␈α∀languages␈α∪under
␈↓different␈α∪guises.␈α∪Usually␈α∪the␈α∀syntax␈α∪of␈α∪the␈α∪language␈α∀is␈α∪sufficiently␈α∪obfuscatory␈α∪that␈α∀the␈α∪true
␈↓behavior␈α~and␈α→implications␈α~of␈α→devices␈α~like␈α→functional␈α~arguments␈α→is␈α~misunderstood.␈α→Faulty
␈↓implementations␈α⊂usually␈α⊂result.␈α⊂In␈α⊂LISP␈α⊂the␈α∂problem␈α⊂␈↓↓and␈α⊂the␈α⊂solution␈↓␈α⊂appear␈α⊂with␈α∂exceptional
␈↓clarity.

␈↓Functional␈α∂arguments␈α∂may␈α∞be␈α∂exploited␈α∂to␈α∞describe␈α∂very␈α∂general␈α∞control␈α∂structures.␈α∂ We␈α∂will␈α∞say
␈↓more␈α
about␈α
this␈α
application␈α
later.

␈↓Finally,␈α
here␈α
is␈α
a␈α
sketch␈α
of␈α
the␈α
abstract␈α
structure␈α
of␈α
the␈α
current␈α
␈↓αeval␈↓.




________________
␈↓α␈↓π 5␈↓α␈α
st␈↓␈α
is␈α
not␈α
really␈α
empty.␈α
Obviously␈α
it␈α
contains␈α
the␈α
function␈α
definitions.
␈↓␈↓↓64  Evaluation␈↓ &4.12␈↓



␈↓αeval <= λ[[exp;environ]
␈↓α␈↓ αx[isvar[exp] → value[exp;environ];
␈↓α␈↓ αx isconst[exp] → denote[exp];
␈↓α␈↓ αx iscond[exp] → evcond[exp;environ];
␈↓α␈↓ αx isfun[exp] → makeclosure[exp;environ];
␈↓α␈↓ αx isfunc+args[exp] → apply[func[exp];list_of_evaled_args[exp;environ];environ]]

␈↓α␈↓where:␈↓α
␈↓αapply <= λ[[fn;args,environ]
␈↓α␈↓ αx[isfunname[fn] →  ....
␈↓α␈↓ αx islambda[fn] → eval[body[fn];newenviron[vars[fn];args;environ]]
␈↓α␈↓ αx isclosure[fn] → apply[func␈↓β1␈↓α[fn];args;evn[fn]]

␈↓α␈↓ αx      ....          ..... ]]


␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓I.␈α
 What␈α
changes␈α
should␈α
be␈α
made␈α
to␈α
the␈α
LISP␈α
syntax␈α
equations␈α
to␈α
allow␈α
functional␈α
arguments.

␈↓II.␈αUse␈α␈↓αfoo␈↓␈αon␈αpage␈α60␈αto␈αdefine␈αa␈αfunction␈αwhich␈αcomputes␈αfactorial␈αwithout␈αusing␈α␈↓αlabel␈↓␈αor␈αexplicit
␈↓calls␈α
on␈α
the␈α
evaluator.

␈↓III.␈α
Extend␈α
␈↓αeval␈↓␈α
and␈α
friends␈α
to␈α
functional␈α
arguments.



␈↓␈↓ ¬R␈↓↓4.13  special forms␈↓


␈↓We␈α∪have␈α∪remarked␈α∀that␈α∪the␈α∪evaluation␈α∀scheme␈α∪for␈α∪LISP␈α∀functions␈α∪is␈α∪call-by-value␈α∀and,␈α∪for
␈↓functions␈α⊂with␈α⊂multiple␈α⊂arguments,␈α∂left-to-right␈α⊂evaluation␈α⊂of␈α⊂arguments.␈α∂We␈α⊂have␈α⊂also␈α⊂seen,␈α∂in
␈↓␈↓αQUOTE␈↓␈αand␈α
␈↓αCOND␈↓,␈αthat␈αnot␈α
all␈αforms␈αto␈α
be␈αevaluated␈αin␈α
LISP␈αfall␈αwithin␈α
this␈αcategory.␈α We␈α
have
␈↓already␈αnoted␈αon␈αpage␈α44␈α
that␈α␈↓αQUOTE␈↓␈αand␈α␈↓αCOND␈↓␈αare␈α
␈↓↓not␈↓␈αtranslates␈αof␈αfunctions.␈αClearly␈αwe␈α
don't
␈↓evaluate␈α
the␈α
arguments␈α
to␈α
␈↓α(QUOTE␈α
...)␈↓;␈α
indeed␈αthe␈α
purpose␈α
of␈α
␈↓αQUOTE␈↓␈α
was␈α
to␈α
␈↓↓stop␈↓␈αevaluation.␈α
 Also
␈↓the␈α∂`arguments'␈α∂to␈α∂␈↓αCOND␈↓␈α∂are␈α∂handled␈α∞differently;␈α∂their␈α∂evaluation␈α∂was␈α∂handled␈α∂by␈α∂␈↓αevcond␈↓.␈α∞ We
␈↓therefore␈α
have␈α
called␈α
␈↓αQUOTE␈↓␈α
and␈α
␈↓αCOND␈↓␈α
␈↓↓special␈αforms␈↓.␈α
 We␈α
would␈α
like␈α
to␈α
discuss␈α
special␈αforms␈α
as
␈↓a␈α
generally␈α
useful␈α
technique.

␈↓Consider␈α
the␈α
predicates,␈α
␈↓αand␈↓␈α
and␈α
␈↓αor␈↓.␈α
We␈α
might␈α
wish␈α
to␈α
define␈α
␈↓αand␈↓␈α
to␈α
be␈α
a␈α
binary␈α
predicate␈α
such
␈↓that␈α
␈↓αand␈↓␈α
is␈α
true␈αjust␈α
in␈α
case␈α
␈↓↓both␈↓␈α
arguments␈αevaluate␈α
to␈α
␈↓αT␈↓;␈α
and␈α
define␈α␈↓αor␈↓␈α
to␈α
be␈α
binary␈α
and␈αfalse␈α
just
␈↓in␈α⊂case␈α⊂both␈α∂arguments␈α⊂evaluate␈α⊂to␈α∂␈↓αNIL␈↓.␈α⊂ Notice␈α⊂two␈α∂points.␈α⊂First,␈α⊂there␈α∂is␈α⊂really␈α⊂no␈α⊂reason␈α∂to
␈↓restrict␈αthese␈αpredicates␈α
to␈αbe␈α␈↓↓binary␈↓.␈αReplacing␈α
the␈αwords␈α"binary"␈αby␈α
"n-ary"␈αand␈α"both"␈α
by␈α"all"
␈↓in␈α∂the␈α⊂above␈α∂description␈α⊂has␈α∂the␈α⊂desired␈α∂effect.␈α⊂ Second,␈α∂if␈α⊂we␈α∂evaluate␈α⊂the␈α∂arguments␈α⊂to␈α∂these
␈↓␈↓↓4.13␈↓ 	Sspecial forms     65␈↓


␈↓predicates␈αin␈αsome␈αorder,␈αsay␈αleft-to-right,␈αthen␈αwe␈αcould␈αimmediately␈αdetermine␈αthat␈α␈↓αand␈↓␈αis␈αfalse␈αas
␈↓soon␈αas␈α
we␈αcome␈α
across␈αan␈α
argument␈αwhich␈αevaluates␈α
to␈α␈↓αNIL␈↓;␈α
similarly␈αa␈α
call␈αon␈α
␈↓αor␈↓␈αfor␈αan␈α
arbitrary
␈↓number␈α
of␈α
arguments␈α
can␈α
be␈α
terminated␈α
as␈α
soon␈α
as␈α
we␈α
evaluate␈α
an␈α
argument␈α
giving␈α
value␈α
␈↓αT␈↓.

␈↓But␈α∪if␈α∩we␈α∪insist␈α∩that␈α∪␈↓αand␈↓␈α∪and␈α∩␈↓αor␈↓␈α∪are␈α∩␈↓↓functions␈↓␈α∪we␈α∪can␈α∩take␈α∪advantage␈α∩of␈α∪neither␈α∪of␈α∩these
␈↓observations.␈α∩Functions␈α∩have␈α∩a␈α∩fixed␈α∩number␈α∩of␈α∩arguments,␈α∩all␈α∩of␈α∩which␈α∩are␈α∪evaluated.␈α∩The
␈↓solution␈α
should␈α
be␈α
clear:␈α
define␈α
␈↓αand␈↓␈α
and␈α␈↓αor␈↓␈α
as␈α
special␈α
forms␈α
and␈α
handle␈α
the␈α
evaluation␈αourselves.
␈↓Presently,␈α
the␈α
only␈α
way␈α
to␈α
handle␈α
special␈α
forms␈α
is␈α
to␈α
make␈α
explicit␈α
modifications␈α
to␈α
␈↓αeval␈↓.␈α∞ This␈α
is
␈↓not␈αterribly␈αdifficult␈α(or␈αdesirable).␈αWhen␈αwe␈αsee␈α
a␈αmore␈αrealistic␈αversion␈αof␈α␈↓αeval␈↓␈αand␈αCo.␈αin␈α
Section
␈↓6.15,␈αwe␈αwill␈αhave␈αa␈αsimple␈αway␈αto␈αadd␈αsuch␈αforms.␈αSee␈αalso␈αpage␈α120.

␈↓Recognizers␈α
for␈α
the␈α
predicates␈α
must␈α
be␈α
added␈α
to␈α
␈↓αeval␈↓:
␈↓α␈↓ ∧xeq[car[e];AND] → evand[cdr[e];a];
␈↓α␈↓ ¬∩eq[car[e];OR] → evor[cdr[e];a];
␈↓α␈↓ ¬y.....␈↓      where:

␈↓αevand <= λ[[l;a]␈↓ β8[null[l]→ T;
␈↓α␈↓ β8 eval[car[l];a] → evand[cdr[l];a];
␈↓α␈↓ β8 T → NIL] ]

␈↓αevor <= λ[[l;a]␈↓ β8[null[l] → NIL;
␈↓α␈↓ β8 eval[car[l];a] → T;
␈↓α␈↓ β8 T → evor[cdr[l];a]] ]


␈↓Notice␈αthe␈αexplicit␈α
calls␈αon␈α␈↓αeval␈↓.␈α
This␈αis␈αexpensive,␈α
but␈αcannot␈αbe␈α
helped.␈α Later␈αwe␈α
will␈αshow␈αa␈α
less
␈↓costly␈αway␈αto␈αhandle␈αthose␈α"non-functions"␈αwhich␈αonly␈αhave␈αan␈αindefinite␈αnumber␈αof␈αarguments,␈αall
␈↓of␈αwhich␈α
are␈αto␈α
be␈αevaluated␈α(see␈α
Section␈α6.13␈α
on␈αmacros).

␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓I␈α∞What␈α∞is␈α
the␈α∞difference␈α∞between␈α
a␈α∞special␈α∞form␈α
and␈α∞call-by-name?␈α∞Can␈α
call-by-name␈α∞be␈α∞done␈α
in
␈↓LISP␈α
(without␈α
redefining␈α
␈↓αeval␈↓)?

␈↓II␈α
␈↓αselect␈↓␈α
is␈αa␈α
special␈α
form␈α
to␈αbe␈α
called␈α
as:␈α␈↓αselect[q;q␈↓β1␈↓α;e␈↓β1␈↓α; ... ;q␈↓βn␈↓α;e␈↓βn␈↓α;e]␈↓␈α
and␈α
to␈α
be␈αevaluated␈α
as␈α
follows:␈α␈↓αq␈↓␈α
is
␈↓evaluated;␈α
the␈α␈↓αq␈↓βi␈↓'s␈α
are␈αevaluated␈α
from␈αleft␈α
to␈αright␈α
until␈αone␈α
is␈αfound␈α
with␈αthe␈α
value␈αof␈α
␈↓αq␈↓.␈αThe␈α
value
␈↓of␈α␈↓αselect␈↓␈αis␈αthe␈αvalue␈αof␈αthe␈αcorresponding␈α␈↓αe␈↓βi␈↓.␈αIf␈αno␈αsuch␈α␈↓αq␈↓βi␈↓␈αis␈αfound␈αthe␈αvalue␈αof␈α␈↓αselect␈↓␈αis␈αthe␈αvalue
␈↓of␈α␈↓αe␈↓.␈α
␈↓αselect␈↓␈αis␈α
a␈αprecursor␈αof␈α
the␈α␈↓↓case␈α
statement␈↓.␈αSee␈α
page␈α70.␈α Add␈α
a␈αrecognizer␈α
to␈α␈↓αeval␈↓␈α
to␈αhandle
␈↓␈↓αselect␈↓␈α
and␈α
write␈α
a␈α
function␈α
to␈α
perform␈α
the␈α
evaluation␈α
of␈α
␈↓αselect␈↓.
␈↓␈↓↓66  Evaluation␈↓ #4.14␈↓


␈↓␈↓ ¬;␈↓↓4.14  The ␈↓αprog␈↓↓-feature␈↓α


␈↓Recursion␈αseems␈αa␈αbit␈αsupernatural,␈αbut␈αit␈αis␈αlegal,␈αmechanizable␈αand␈αrather␈αcool.␈α There␈αis␈αanother
␈↓similar␈α∀binding␈α∀process␈α∪occurring␈α∀in␈α∀LISP.␈α∀ It␈α∪is␈α∀connected␈α∀with␈α∪an␈α∀iterative␈α∀style␈α∀of␈α∪LISP
␈↓programming␈α
called␈α
the␈α
␈↓αprog␈↓-feature.

␈↓First␈α⊗a␈α⊗general␈α∃disclaimer:␈α⊗Some␈α⊗pure␈α∃("For␈α⊗my␈α⊗next␈α∃trick␈α⊗I'll␈α⊗walk␈α∃on␈α⊗the␈α⊗water")␈α∃LISP
␈↓programmers␈α∞feel␈α∞that␈α∂there␈α∞is␈α∞something␈α∞slightly␈α∂obscene␈α∞about␈α∞writing␈α∞iterative␈α∂style␈α∞programs.
␈↓This␈α
isn't␈α
quite␈α
true,␈α
but␈α
there␈α
are␈α
some␈α
good␈α
reasons␈α
for␈α
emphasizing␈α
recursion.

␈↓␈↓↓1.␈↓␈α∞Anyone␈α∞can␈α∂write␈α∞an␈α∞iterative␈α∂scheme.␈α∞ Recursion␈α∞is␈α∂a␈α∞powerful␈α∞tool␈α∂and␈α∞very␈α∞possibly␈α∂a␈α∞new
␈↓programming␈α∞technique␈α∞for␈α
you.␈α∞ You␈α∞should␈α
exercise␈α∞your␈α∞skill␈α
and␈α∞resort␈α∞to␈α
the␈α∞␈↓αprog␈↓␈α∞as␈α∞a␈α
last
␈↓resort.

␈↓␈↓↓2.␈↓␈αTwo␈αof␈αthe␈αusual␈αtrappings␈αof␈αiteration␈αare␈α␈↓↓labels␈↓␈αand␈α␈↓↓go-to␈↓'s.␈αThese␈αare␈αtruly␈αtools␈αof␈αthe␈αdevil.
␈↓In␈α
recent␈α
years␈α
several␈α
studies␈α
by␈α
reasonable␈αmen␈α
have␈α
shown␈α
that␈α
algorithms␈α
which␈α
resort␈αto␈α
labels
␈↓and␈αgotos␈αtend␈αto␈α
be␈αharder␈αto␈αread,␈αharder␈α
to␈αmodify,␈αsloppy␈αin␈α
their␈αanalysis␈αof␈αthe␈αprocess␈α
being
␈↓coded,␈αand␈αgenerally␈αugly.␈α The␈αlabel␈αand␈αgoto␈αhack␈αinvites␈αpatching␈αover␈αpoor␈αanalysis␈αinstead␈αof
␈↓reanalyzing␈α
the␈α
problem.

␈↓␈↓↓3.␈↓␈α
With␈α
the␈α
control␈α
of␈α
the␈α
loop␈α
structure␈α
(either␈α
recursion␈α
or␈α
some␈α
kind␈α
of␈α
controlled␈α
iteration,␈α
e.g.,␈α
a
␈↓FOR␈α∩or␈α∪WHILE␈α∩statement)␈α∩in␈α∪the␈α∩hands␈α∪of␈α∩the␈α∩language␈α∪rather␈α∩than␈α∩in␈α∪the␈α∩hands␈α∪of␈α∩the
␈↓programmer,␈α∂the␈α∞static␈α∂text␈α∂and␈α∞the␈α∂dynamic␈α∂flow␈α∞of␈α∂the␈α∂execution␈α∞have␈α∂a␈α∂parallel␈α∞relationship.
␈↓This␈α⊃should␈α∩have␈α⊃a␈α⊃beneficial␈α∩effect␈α⊃for␈α∩studies␈α⊃investigating␈α⊃the␈α∩provability␈α⊃of␈α∩properties␈α⊃of
␈↓programs.

␈↓Now␈α
that␈α
this␈α
has␈α
been␈α
said,␈α
here's␈α
our␈α
discussion␈α
of␈α
␈↓αprog␈↓s.

␈↓Many␈α∩algorithms␈α⊃present␈α∩themselves␈α⊃more␈α∩naturally␈α⊃as␈α∩iterative␈α⊃schemes.␈α∩ Recall␈α∩the␈α⊃recursive
␈↓computation␈α⊃of␈α⊃the␈α⊃length␈α⊃of␈α⊂a␈α⊃list␈α⊃given␈α⊃on␈α⊃page␈α⊂21.␈α⊃␈↓αlength␈↓␈α⊃seems␈α⊃inordinately␈α⊃complex;␈α⊂our
␈↓sympathies␈α
lie␈αmore␈α
with␈α␈↓αlength␈↓β1␈↓.␈α
 Even␈αthis␈α
is␈α
not␈αas␈α
straightforward␈αas␈α
you␈αwould␈α
expect␈αfor␈α
such
␈↓a␈α
simple␈α
calculation.␈α
 Rather,␈α
consider␈α
the␈α
following:

␈↓␈↓↓1.␈↓␈α
Set␈α
a␈α
pointer␈α
to␈α
the␈α
given␈α
list.␈α
 Set␈α
a␈α
counter␈α
to␈α
zero.

␈↓␈↓↓2.␈↓␈α
If␈α
the␈α
list␈α
is␈α
empty,␈α
return␈α
as␈α
value␈α
of␈α
the␈α
computation,␈α
the␈α
current␈α
value␈α
in␈α
the␈α
counter.

␈↓␈↓↓3.␈↓␈α
Otherwise,␈α
increment␈α
the␈α
counter␈α
by␈α
one.

␈↓␈↓↓4.␈↓␈α
Set␈α
the␈α
pointer␈α
to␈α
the␈α
cdr␈α
of␈α
the␈α
list.

␈↓␈↓↓5.␈↓␈α
Go␈α
to␈α
line␈α
2.

␈↓The␈α
new␈α
ingredients␈α
here␈α
are:
␈↓␈↓↓4.14␈↓ 	(The ␈↓αprog␈↓↓-feature     67␈↓α


␈↓␈↓↓1.␈↓␈α
An␈α
assignment␈α
statement:␈α
"set␈α
a␈α
...".

␈↓␈↓↓2.␈↓␈α
Access␈α
to␈α
some␈α
new␈α
cells:␈α
the␈α
pointer,␈α
the␈α
counter.

␈↓␈↓↓3.␈↓␈α
Sequencing␈α
(albeit␈α
usually␈α
implied)␈α
between␈α
statements:␈α
line␈α
␈↓↓1␈↓,␈α
then␈α
line␈α
␈↓↓2␈↓...

␈↓␈↓↓4.␈↓␈α
Gotos␈α
and␈α
labels.

␈↓␈↓↓5.␈↓␈α
An␈α
explicit␈α
exit␈α
from␈α
the␈α
procedure:␈α
line␈α
␈↓↓2␈↓.

␈↓Here␈α
is␈α
the␈α
LISP␈α
␈↓αprog␈↓␈α
version␈α
of␈α
the␈α
length␈α
function:

␈↓αlength <= λ[[x]prog[[l;c]
␈↓α␈↓ β(␈↓ βHl ← x;
␈↓α␈↓ β(␈↓ βHc ← 0;
␈↓α␈↓ β(a␈↓ βH[null[l] → return[c]];
␈↓α␈↓ β(␈↓ βHl ← cdr[l];
␈↓α␈↓ β(␈↓ βHc ← c+1;
␈↓α␈↓ β(␈↓ βHgo[a]] ]


␈↓A␈αfew␈α
points␈αshould␈α
be␈αmade:␈αThe␈α
␈↓αprog␈↓-variables,␈α␈↓αl␈↓␈α
and␈α␈↓αc␈↓,␈αare␈α
local␈αvariables.␈α
 That␈αis,␈α
they␈αonly
␈↓have␈α∂meaning␈α∂within␈α∂this␈α∂definition␈α∂of␈α∂␈↓αlength␈↓.␈α∂If␈α∂they␈α∂appeared␈α∂in␈α∂some␈α∂program␈α⊂which␈α∂called
␈↓␈↓αlength␈↓,␈α∞then␈α∞the␈α∞right␈α∞thing␈α∞would␈α∞happen;␈α∞the␈α∞old␈α∞values␈α∞would␈α∞be␈α∞saved␈α∞(like␈α∞λ-bindings)␈α∞and
␈↓then␈αrestored␈αafter␈αthe␈αcall␈αon␈α␈↓αlength␈↓␈αhas␈αbeen␈αcompleted.␈αAmong␈αother␈αthings,␈αthis␈αallows␈α␈↓αprog␈↓s␈αto
␈↓be␈α
used␈α
recursively.

␈↓Though␈α∞assignment␈α∞statements␈α∞are␈α
normally␈α∞executed␈α∞for␈α∞their␈α
effect␈α∞on␈α∞the␈α∞environment␈α∞--␈α
they
␈↓have␈αside-effects,␈αassignments␈αin␈αLISP␈αalso␈αhave␈αa␈αvalue.␈α The␈αvalue␈αof␈αan␈αassignment␈αis␈αthe␈αvalue
␈↓of␈α
the␈α
right-hand-side.

␈↓Conditional␈α
expressions␈α
have␈α
a␈α
slightly␈α
different␈α
effect␈α
inside␈α
␈↓αprog␈↓s.␈α
If␈α
none␈α
of␈α
the␈α
predicates␈αin␈α
the
␈↓conditional␈α
are␈α
true,␈α
then␈α
the␈α
statement␈α
following␈α
the␈α
conditional␈α
is␈α
executed.

␈↓Labels␈αare␈αclear;␈αthey␈αare␈αidentifiers.␈αLabels␈αare␈αlocal,␈αthat␈αis␈αmust␈αbe␈αfound␈αwithin␈αthe␈αbody␈αof␈α
the
␈↓current␈α
␈↓αprog␈↓.␈α
Labels␈α
may␈α
conflict␈α
with␈αthe␈α
λ-variables␈α
or␈α
␈↓αprog␈↓-variables;␈α
the␈α
evaluator␈α
for␈α␈↓αprog␈↓s␈α
can
␈↓resolve␈α
the␈α
conflicts␈α
by␈α
context.

␈↓␈↓αgo␈↓␈α∂is␈α∂a␈α∂little␈α∂more␈α∂complicated.␈α∞␈↓αgo␈↓␈α∂is␈α∂a␈α∂special␈α∂form.␈α∂If␈α∂the␈α∞argument␈α∂to␈α∂␈↓αgo␈↓␈α∂is␈α∂␈↓↓atomic␈↓␈α∂then␈α∂it␈α∞is
␈↓interpreted␈αas␈αa␈α(local)␈αlabel;␈αotherwise,␈αthe␈αargument␈αis␈α␈↓↓evaluated␈↓␈αand␈αthe␈αresult␈αof␈αthe␈αevaluation
␈↓is␈α
interpreted␈αas␈α
a␈αlabel.␈α
 This␈αresults␈α
in␈αa␈α
useful␈αprogramming␈α
trick.␈α Let␈α
␈↓αl␈↓␈αbe␈α
a␈αlist␈α
of␈αdotted␈α
pairs,
␈↓each␈αof␈α
the␈αform,␈α␈↓α(␈↓␈α
object␈↓βi␈↓␈α.␈αlabel␈↓βi␈↓α)␈↓.␈α
At␈αeach␈αlabel␈↓βi␈↓␈α
we␈αbegin␈αa␈α
piece␈αof␈αprogram␈α
to␈αbe␈αexecuted␈α
when
␈↓object␈↓βi␈↓␈α
has␈α
been␈α
recognized.␈α
 Then␈α
the␈α
construct:

␈↓␈↓ ¬<␈↓αgo[cdr[assoc[x;l]]]␈↓ (*1*), 
␈↓␈↓↓68  Evaluation␈↓ #4.14␈↓


␈↓can␈αbe␈αused␈αto␈α"dispatch"␈αto␈αthe␈αappropriate␈αcode␈αwhen␈α␈↓αx␈↓␈αis␈αone␈αof␈αthe␈αobject␈↓βi␈↓.␈αThis␈αis␈αan␈αinstance
␈↓of␈α⊂␈↓↓table-driven␈↓␈α⊂programming.␈α⊂ This␈α⊂programming␈α⊂trick␈α⊂shows␈α⊂"go-to"␈α⊂programming␈α⊂in␈α⊃its␈α⊂most
␈↓pervasive␈α
form.␈α The␈α
blocks␈αof␈α
code␈αdispatched␈α
to␈αcan␈α
be␈αdistributed␈α
throughout␈αthe␈α
body␈α
of␈αthe
␈↓␈↓αprog␈↓.␈αEach␈αblock␈αof␈αcode␈αwill␈αusually␈αbe␈αfollowed␈αby␈αa␈α␈↓αgo␈↓␈αback␈αto␈αthe␈αcode␈αinvolving␈αequation␈α*1*
␈↓(above).␈α∞In␈α∞fact␈α∞the␈α∞argument␈α∞␈↓αl␈↓␈α
in␈α∞*1*␈α∞may␈α∞be␈α∞␈↓↓global␈↓␈α∞to␈α
the␈α∞␈↓αprog␈↓-body.␈α∞ The␈α∞general␈α∞effect␈α∞is␈α
to
␈↓make␈αa␈α␈↓αprog␈↓␈αwhich␈αis␈αvery␈αdifficult␈αto␈αunderstand.␈α The␈αLISP␈α␈↓αselect␈↓␈α(page␈α65)␈αwill␈αhandle␈αmany␈αof
␈↓the␈α∀possible␈α∀applications␈α∀of␈α∀this␈α∀coding␈α∀trick␈α∀and␈α∀result␈α∀in␈α∀a␈α∀more␈α∀readable␈α∃program.␈α∀The
␈↓case-statement␈α∞(page␈α∞70)␈α∞present␈α∞in␈α∞some␈α∞other␈α
languages␈α∞is␈α∞also␈α∞a␈α∞better␈α∞means␈α∞of␈α∞handling␈α
this
␈↓problem.

␈↓The␈α
␈↓αreturn␈↓␈α
statement␈α
evaluates␈α
its␈α
argument,␈α
and␈α
returns␈α
that␈α
value␈α
as␈α
the␈α
value␈α
of␈α
the␈α
␈↓αprog␈↓.

␈↓When␈αa␈α␈↓αprog␈↓␈αis␈αentered␈αthe␈α␈↓αprog␈↓-␈α(local)␈αvariables␈αare␈αinitialized␈αto␈α␈↓αNIL␈↓.␈α The␈αbody␈αof␈αthe␈α␈↓αprog␈↓␈αis
␈↓a␈α
sequence␈α
of␈α
statements␈α∞and␈α
labels.␈α
Statements␈α
are␈α
executed␈α∞sequentially␈α
unless␈α
a␈α
␈↓αgo␈↓␈α
or␈α∞␈↓αreturn␈↓␈α
is
␈↓evaluated.␈αIf␈αa␈α␈↓αprog␈↓␈αruns␈αout␈αof␈αstatements␈αthen␈α␈↓αNIL␈↓␈αis␈αreturned.␈α Returning␈αfrom␈αa␈α␈↓αprog␈↓␈αunbinds
␈↓the␈α
local␈α
variables.

␈↓Now␈α∞to␈α∞the␈α∞problem␈α∞of␈α∞translating␈α∞␈↓αprog␈↓s␈α∞into␈α∞a␈α∞s-expression␈α∞representation.␈α∞ We␈α∞need␈α∞be␈α∂a␈α∞little
␈↓careful.␈α
First␈α
notice␈α
that␈α
␈↓αprog␈↓␈α
is␈α
a␈α
␈↓↓special␈α
form␈↓␈α
(like␈α
␈↓αCOND␈↓␈α
and␈α
␈↓αQUOTE␈↓).␈α
The␈α
construct:

␈↓␈↓ ∧|␈↓αprog[[l;c]....]␈↓ will be translated to: 

␈↓␈↓ ¬e␈↓α(PROG(L C) ...)␈↓. 

␈↓But␈α␈↓αPROG␈↓␈αis␈αnot␈αthe␈αtranslate␈αof␈αa␈αfunction␈αany␈αmore␈αthan␈α␈↓α(QUOTE␈α...)␈↓␈αor␈α␈↓α(COND␈α...)␈↓␈αis.␈α So␈αthe
␈↓body␈α
of␈α
the␈α
␈↓αprog␈↓␈α
must␈α
be␈α
handled␈α
specially␈α
by␈α
a␈α
new␈α
piece␈α
of␈α
the␈α
evaluator.

␈↓Similarly␈α
we␈αmust␈α
be␈αcareful␈α
about␈αthe␈α
interpretation␈αof␈α
←.␈αFirst,␈α
we␈αwill␈α
write␈α␈↓αx␈α
←␈αy␈↓␈α
in␈αprefix␈α
form:
␈↓␈↓α←[x;y]␈↓.␈α
Now,␈α
using␈α
our␈α
usual␈α
LISP␈α
technique␈α
we␈α
might␈α
map␈α
this␈α
to:
␈↓␈↓ β<␈↓α(SET X Y).␈↓   Is ← or ␈↓αSET␈↓ a function in the usual LISP sense?

␈↓Not␈αlikely;␈αfor␈α
if␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓␈α
have␈αvalues␈α␈↓α2␈↓␈α
and␈α␈↓α3␈↓,␈αfor␈αexample,␈α
then␈αthe␈αcall-by-value␈αinterpretation␈α
of
␈↓␈↓α←[x;y]␈↓␈α
would␈α
say␈α
␈↓α←[2;3]␈↓.␈α
This␈α
was␈α
not␈α∞our␈α
intention,␈α
hopefully.␈α
 What␈α
do␈α
we␈α
want?␈α
 We␈α∞want␈α
to
␈↓evaluate␈αthe␈αsecond␈α
argument␈αto␈α←␈αwhile␈α
stopping␈αthe␈αevaluation␈αof␈α
the␈αfirst␈αargument.␈α
But␈αLISP
␈↓does␈α∂have␈α∞a␈α∂device␈α∞for␈α∂stopping␈α∞evaluation:␈α∂␈↓αQUOTE␈↓.␈α∂So␈α∞we␈α∂can␈α∞define␈α∂␈↓αSET␈↓␈α∞as␈α∂a␈α∂normal␈α∞LISP
␈↓function,␈α
provided␈α
we
␈↓␈↓ ∧ translate       ␈↓αx ← y␈↓    as ␈↓α(SET (QUOTE X) Y)␈↓.

␈↓For␈αexample,␈αlook␈α
at␈αthe␈αevaluation␈αof␈α
␈↓α(SET␈α(QUOTE␈αX)(PLUS␈αX␈α
1)).␈↓␈αAssume␈αthe␈α
current␈αvalue
␈↓of␈α␈↓αX␈↓␈αis␈α␈↓α2.␈α SET␈↓␈αis␈αa␈αfunction;␈αevaluate␈αthe␈αarguments␈αfrom␈αleft␈αto␈αright.␈αThe␈αvalue␈αof␈α␈↓α(QUOTE␈αX)␈↓
␈↓is␈α
␈↓αX␈↓;␈α
the␈α
value␈α
of␈α
␈↓α(PLUS␈α
X␈α
1)␈↓␈α
is␈α
␈↓α3␈↓;␈α
assign␈α
␈↓α3␈↓␈α
to␈α
␈↓αX␈↓.

␈↓Question:␈α∪what␈α∩does␈α∪␈↓α(SET␈α∪Z␈α∩(PLUS␈α∪X␈α∪1))␈↓␈α∩mean?␈α∪ Well␈α∩if␈α∪the␈α∪current␈α∩value␈α∪of␈α∪variable␈α∩␈↓αz␈↓
␈↓(represented␈α∞by␈α∞␈↓αZ␈↓)␈α∂is␈α∞an␈α∞identifier␈α∞(non-numeric␈α∂atom),␈α∞then␈α∞␈↓α(SET␈α∞Z␈α∂(PLUS␈α∞X␈α∞1))␈↓␈α∂makes␈α∞sense.
␈↓Assume␈α
the␈αcurrent␈α
value␈αof␈α
␈↓αZ␈↓␈αis␈α
␈↓αA␈↓,␈α
then␈αthe␈α
effect␈αof␈α
the␈α␈↓αSET␈↓␈α
statement␈α
is␈αto␈α
assign␈αthe␈α
value␈α␈↓α3␈↓␈α
to
␈↓␈↓αA␈↓.
␈↓␈↓↓4.14␈↓ 	(The ␈↓αprog␈↓↓-feature     69␈↓α


␈↓Normally␈αwhen␈α
you␈αare␈αmaking␈α
assignments,␈αyou␈αwant␈α
to␈αassign␈αto␈α
a␈α␈↓↓name␈↓␈αand␈α
not␈αa␈α
␈↓↓value␈↓;␈αthus
␈↓you␈α
will␈α
be␈α
using␈α
the␈α
form

␈↓␈↓ ¬D␈↓α(SET (QUOTE ...) ...).␈↓ 

␈↓As␈α
a␈α
convenience␈α
an␈α
abbreviation,␈α
␈↓αSETQ␈↓,␈α
has␈α
been␈α
made␈α
available:

␈↓␈↓ ∧5␈↓α(SETQ ...  ... )␈↓ means ␈↓α (SET (QUOTE ...) ...).␈↓ 

␈↓Again␈αnote␈α
that␈α␈↓αSETQ␈↓␈α
is␈αnot␈α
(the␈αtranslate␈α
of)␈αa␈α
function.␈αIt␈α
may␈αbe␈α
defined␈αas␈α
a␈αspecial␈α
form␈αor
␈↓consider␈α
as␈α
a␈α
notational␈α
convenience␈α
like␈α
␈↓αlist␈↓.

␈↓Here␈α
is␈α
a␈α
translation␈α
of␈α
the␈α
body␈α
of␈α
the␈α
␈↓αprog␈↓␈α
version␈α
of␈α
␈↓αlength:␈↓
␈↓α␈↓ αX(LAMBDA(X)
␈↓α␈↓ αX␈↓ βH(PROG(L C)
␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ L X)
␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ C 0)
␈↓α␈↓ αX␈↓ βHA␈↓ βx(COND ((NULL L)(RETURN C)))
␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ L(CDR L))
␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ C (ADD1 C))
␈↓α␈↓ αX␈↓ βH␈↓ βx(GO A) ))




␈↓␈↓ ¬π␈↓↓4.15  Problems involving ␈↓αprog␈↓↓␈↓α


␈↓I␈α
Write␈α
␈↓αprog␈↓-versions␈α
of␈α
the␈α
following␈α
functions␈α
(or␈α
predicates).

␈↓␈↓↓1.␈↓α␈αmember[x;y]<=␈α...␈α
:␈αx␈↓␈αis␈α
atomic;␈α␈↓αy␈↓␈αis␈α
a␈αlist␈αof␈α
atoms.␈α ␈↓αmember␈↓␈αis␈α
to␈αreturn␈α␈↓αT␈↓␈α
just␈αin␈αthe␈α
case␈αthat␈α␈↓αx␈↓␈α
is
␈↓␈↓ α_one␈α
of␈α
the␈α
elements␈α
in␈α
␈↓αy␈↓.

␈↓␈↓↓2.␈↓␈α
The␈α
factorial␈α
function.

␈↓␈↓↓3.␈↓α␈α∞delete[x;y]<=␈α∞...␈α∞:␈α∞x␈↓␈α∞is␈α∞atomic;␈α∞␈↓αy␈↓␈α∞is␈α∞a␈α∞list␈α∞of␈α∞atoms.␈α∞ ␈↓αdelete␈↓␈α∞is␈α∞to␈α∞return␈α∞a␈α∞list␈α∞which␈α∞looks␈α∞like␈α∞␈↓αy␈↓,
␈↓␈↓ α_except␈α
all␈α
occurrences␈α
of␈α
␈↓αx␈↓␈α
have␈α
been␈α
deleted.

␈↓␈↓↓4.␈↓␈α
The␈α
␈↓αappend␈↓␈α
function.

␈↓␈↓↓5.␈↓α␈α
last[x]<=␈α
...␈α
:␈α
x␈↓␈α
is␈α
a␈α
non-empty␈α
list.␈α
 ␈↓αlast␈↓␈α
is␈α
to␈α
return␈α
the␈α
last␈α
element␈α
in␈α
␈↓αx␈↓.

␈↓␈↓↓6.␈↓␈α
Now␈α
write␈α
the␈α
Sexpr␈α
translations␈α
of␈α
each␈α
of␈α
your␈α
functions.

␈↓II␈α
What␈α
is␈α
necessary␈α
to␈α
extend␈α
the␈α
evaluator␈α
to␈α
recognize␈α
␈↓αprog␈↓␈α
and␈α
friends.
␈↓␈↓↓70  Evaluation␈↓ %4.15␈↓


␈↓III␈α
The␈α␈↓αgo[cdr[...]]␈↓-construct␈α
on␈α
page␈α67␈α
is␈αbetter␈α
handled␈α
with␈αa␈α
␈↓↓case␈αstatement␈↓.␈α
A␈α
typical␈αsyntax
␈↓␈↓ α_for␈α
such␈α
might␈α
be:

␈↓␈↓ ∧rcase<index>of <form␈↓β1␈↓>; ...<form␈↓βn␈↓>. 

␈↓<index>␈α⊃is␈α⊃to␈α⊂evaluate␈α⊃to␈α⊃an␈α⊂integer,␈α⊃i.␈α⊃Where␈α⊂0<i≤n.␈α⊃The␈α⊃i␈↓πth␈↓␈α⊂<form>␈α⊃of␈α⊃the␈α⊃case-statement␈α⊂is
␈↓␈↓ α_executed,␈α⊃and␈α⊃is␈α⊃the␈α⊃value␈α⊃of␈α∩the␈α⊃statement.␈α⊃ Construct␈α⊃a␈α⊃reasonable␈α⊃case␈α∩statement␈α⊃and
␈↓␈↓ α_extend␈α
the␈α
evaluator␈α
to␈α
recognize␈α
it.

␈↓IV␈α
This␈α
problem␈α
concerns␈α
left-hand␈α
values.␈α
 Some␈α
languages␈α
allow␈α
constructs␈α
like:
␈↓␈↓ βP(␈↓↓if␈↓ p(x) ␈↓↓then␈↓ x ␈↓↓else␈↓ y) ← exp, which is to mean the same as:

␈↓␈↓ ∧|␈↓↓if␈↓ p(x) ␈↓↓then␈↓ x← exp ␈↓↓else␈↓ y ← exp

␈↓Can␈α
such␈αa␈α
construct␈α
be␈αwritten␈α
in␈αLISP.␈α
 How␈α
natural␈αis␈α
it?␈α How␈α
important␈α
or␈αuseful␈α
is␈α
such␈αa
␈↓␈↓ α_facility?

␈↓V␈α⊃Compare␈α⊂the␈α⊃␈↓αprog␈↓-version␈α⊂of␈α⊃␈↓αlength␈↓␈α⊂on␈α⊃page␈α⊂67␈α⊃with␈α⊂␈↓αlength␈↓β1␈↓␈α⊃on␈α⊂page␈α⊃21.␈α⊂Do␈α⊃you␈α⊃see␈α⊂any
␈↓␈↓ α_interesting␈α
relationships?␈α
Can␈α
you␈α
generalize?



␈↓␈↓ ¬W␈↓↓4.16  In retrospect␈↓


␈↓We␈α∞will␈α∞begin␈α∞with␈α∞a␈α∞sketch␈α∞of␈α∞the␈α∞LISP␈α∞evaluator␈α∞as␈α∞it␈α∞would␈α∞now␈α∞appear:␈α∞basic␈α∞␈↓αeval␈↓␈α∞plus␈α∞the
␈↓additional␈α∞artifacts␈α∞for␈α∞␈↓αlabel,␈α∞function␈↓,␈α∞and␈α∞␈↓αprog␈↓.␈α∞ This␈α∞evaluation␈α∞process␈α∞is␈α∞very␈α∞important␈α∞and,
␈↓though␈αit␈αis␈αperhaps␈αnew␈αmaterial,␈αshould␈αbe␈αappear␈αquite␈αintuitive␈αin␈αretrospect.␈α␈↓αeval␈↓␈αand␈αfriends
␈↓are␈α⊂not␈α⊂to␈α⊂be␈α⊂memorized.␈α⊂If␈α⊂you␈α⊃cannot␈α⊂write␈α⊂functions␈α⊂equivalent␈α⊂to␈α⊂␈↓αeval␈↓,␈α⊂then␈α⊂you␈α⊃have␈α⊂not
␈↓understood␈α
LISP␈α
evaluation.

␈↓Finally,␈αwe␈αwill␈α
examine␈αsome␈αof␈α
the␈αweaknesses␈αpresent␈α
in␈αLISP.␈αThere␈α
are␈αalternatives␈αto␈αsome␈α
of
␈↓the␈α
LISP␈α
techniques␈α
and␈α
there␈α
are␈α
some␈α
things␈α
which,␈α
in␈α
retrospect,␈α
LISP␈α
could␈α
have␈α
done␈α
better.

␈↓First,␈αthe␈αbasic␈αevaluator.␈αThere␈αare␈αtwo␈αarguments␈αto␈α␈↓αeval␈↓:␈αa␈α␈↓↓form␈↓␈α␈↓π 6␈↓,␈αthat␈αis␈αan␈αexpression␈αwhich
␈↓can␈α
be␈α
evaluated;␈α
and␈α
second,␈α
an␈α
association␈α
list␈αor␈α
␈↓↓symbol␈α
table␈↓.␈α
If␈α
the␈α
form␈α
is␈α
a␈α
number,␈αthe␈α
atom
␈↓␈↓αT␈↓␈αor␈α␈↓αNIL␈↓,␈αreturn␈αthat␈αform.␈αIf␈αthe␈αform␈αis␈αa␈αvariable,␈αfind␈αthe␈αvalue␈αof␈αthe␈αvariable␈αin␈αthe␈α
current
␈↓table␈αor␈αenvironment.␈α If␈αthe␈α
form␈αis␈αa␈α(non␈αnumeric)␈α
S-expression␈αconstant,␈αreturn␈αthat␈αconstant.␈α
If
␈↓the␈α∂form␈α⊂is␈α∂a␈α∂conditional␈α⊂expression,␈α∂then␈α∂evaluate␈α⊂it␈α∂according␈α∂to␈α⊂the␈α∂semantics␈α⊂of␈α∂conditional
␈↓expressions.␈α⊂If␈α⊂the␈α⊂form␈α⊂is␈α⊂a␈α⊂␈↓αprog␈↓,␈α⊂evaluate␈α∂the␈α⊂body␈α⊂of␈α⊂the␈α⊂␈↓αprog␈↓␈α⊂after␈α⊂binding␈α⊂the␈α⊂local␈α∂␈↓αprog␈↓


________________
␈↓␈↓π 6␈↓␈α∩throughout␈α∩this␈α∩section␈α∩we␈α∩will␈α∩say␈α∩"form",␈α∩"variable",␈α∩"λ-expression",␈α∩etc.␈α∩ rather␈α∩than␈α∩"an
␈↓S-expression␈α⊃representation␈α⊂of␈α⊃a"␈α⊂...␈α⊃"form",␈α⊂"variable",␈α⊃"λ-expression",␈α⊂etc.␈α⊃No␈α⊃confusion␈α⊂should
␈↓result,␈α
but␈α
remember␈α
that␈α
we␈α
␈↓↓are␈↓␈α
speaking␈α
imprecisely.
␈↓␈↓↓4.16␈↓ 	\In retrospect     71␈↓


␈↓variables␈αto␈α␈↓αNIL␈↓␈↓π 7␈↓.␈α
 The␈αform␈αmight␈α
also␈αbe␈αa␈α
functional␈αargument.␈αIn␈α
this␈αcase␈αevaluation␈α
consists
␈↓of␈α
forming␈αthe␈α
closure␈α
of␈αthe␈α
function,␈α
i.e.,␈αassociating␈α
the␈α
current␈αenvironment␈α
with␈α
the␈αfunction.
␈↓In␈αLISP,␈αthis␈αis␈αdone␈αwith␈αthe␈α␈↓αFUNARG␈↓␈αdevice.␈α Any␈αother␈αform␈αseen␈αby␈α␈↓αeval␈↓␈αis␈αassumed␈αto␈αbe␈αa
␈↓function␈αfollowed␈αby␈αarguments.␈α The␈α
arguments␈αare␈αevaluated␈αfrom␈αleft-to-right␈αand␈α
the␈αfunction
␈↓is␈α
then␈α
applied␈α
to␈α
these␈α
arguments.

␈↓The␈α∞part␈α
of␈α∞the␈α∞evaluator␈α
which␈α∞handles␈α∞function␈α
application␈α∞is␈α∞called␈α
␈↓αapply␈↓.␈α∞ ␈↓αapply␈↓␈α∞takes␈α
three
␈↓arguments:␈α
a␈α
function,␈α
a␈α
list␈α
of␈α
evaluated␈α
arguments,␈α
and␈α
the␈α
current␈α
symbol␈α
table.␈α
If␈α
the␈αfunction␈α
is
␈↓one␈α∂of␈α∂the␈α∂five␈α∞LISP␈α∂primitives␈α∂then␈α∂the␈α∞appropriate␈α∂action␈α∂is␈α∂carried␈α∞out.␈α∂If␈α∂the␈α∂function␈α∂is␈α∞a
␈↓λ-expression␈α∂then␈α∂bind␈α⊂the␈α∂formal␈α∂parameters␈α∂(the␈α⊂λ-variables)␈α∂to␈α∂the␈α∂evaluated␈α⊂arguments␈α∂and
␈↓evaluate␈αthe␈αbody␈αof␈αthe␈αfunction.␈αThe␈αfunction␈αmight␈αalso␈αbe␈αthe␈αresult␈αof␈αa␈αfunctional␈αargument
␈↓binding;␈αin␈αthis␈αcase␈αapply␈αthe␈αfunction␈αto␈αthe␈αarguments␈αin␈αthe␈αsaved␈αenvironment␈αrather␈αthan␈αin
␈↓the␈α∞current␈α∞environment.␈α∞ We␈α
might␈α∞also␈α∞be␈α∞applying␈α
the␈α∞label␈α∞operator.␈α∞All␈α
we␈α∞need␈α∞do␈α∞here␈α
is
␈↓apply␈αthe␈αfunction␈αdefinition␈αto␈αthe␈αarguments␈αin␈αthe␈αcurrent␈αcontext␈αaugmented␈αby␈αthe␈αbinding␈αof
␈↓the␈α
function␈α
name␈α
to␈α
its␈α
definition.

␈↓We␈α
have␈α
saved␈αwhat␈α
seems␈α
to␈α
be␈αthe␈α
simplest␈α
for␈α
last.␈αThat␈α
is␈α
the␈α
function␈αis␈α
named;␈α
what␈α
we␈αdo␈α
is
␈↓look␈α
up␈α
the␈α
name␈αof␈α
the␈α
function␈α
in␈αthe␈α
current␈α
environment.␈α
 To␈αlook␈α
something␈α
up␈α
means␈αto␈α
find
␈↓its␈α∞value.␈α∞ Currently␈α∞we␈α∞expect␈α
that␈α∞value␈α∞to␈α∞be␈α∞a␈α
λ-expression,␈α∞which␈α∞is␈α∞then␈α∞applied.␈α
However,
␈↓since␈αfunction␈αnames␈αare␈αjust␈αvariables,␈αthere␈αis␈αno␈αreason␈αthat␈αthe␈αvalue␈αof␈αa␈αfunction␈αname␈αcould
␈↓not␈α∪be␈α∪a␈α∪simple␈α∪value,␈α∩say␈α∪an␈α∪atom,␈α∪and␈α∪␈↓↓that␈↓␈α∩value␈α∪applied␈α∪to␈α∪the␈α∪arguments,␈α∪etc.␈α∩ Indeed
␈↓examination␈α∪of␈α∀␈↓αapply␈↓␈α∪on␈α∪page␈α∀51␈α∪will␈α∪show␈α∀that␈α∪␈↓αapply[X; ((A␈α∪B)) ; ((X . CAR) ... )]␈↓␈α∀␈↓↓will␈↓␈α∪be
␈↓handled␈αcorrectly.␈α The␈αobvious␈αextension␈αof␈αthis␈αidea␈α
is␈αto␈αallow␈αa␈α␈↓↓computed␈αfunction␈↓.␈αThat␈αis,␈α
if
␈↓the␈α∂function␈α∂passed␈α∂to␈α∂apply␈α∂is␈α∂not␈α∂recognized␈α∂as␈α∂one␈α∂of␈α∂the␈α∂preceding␈α∂cases,␈α∂then␈α∂evaluate␈α∞the
␈↓expresssion␈α
until␈α
it␈α
is␈α
recognized.␈α
Thus␈α
we␈α
will␈α
allow␈α
such␈α
forms␈α
as:
␈↓α␈↓ ∧β((CAR (QUOTE (CAR (A . B)))) (QUOTE (A . B)))

␈↓What␈α
conceptual␈αdifficulties␈α
are␈α
present␈αin␈α
LISP␈α
evaluation?␈α One␈α
of␈α
the␈αmore␈α
important␈αdefects␈α
in
␈↓LISP␈α∪is␈α∪its␈α∪inadequate␈α∪handling␈α∪of␈α∪function␈α∪valued␈α∪variables,␈α∪functional␈α∪arguments␈α∀being␈α∪a
␈↓particular␈α∩case.␈α∩LISP␈α∩was␈α∩the␈α∩first␈α∩language␈α∪to␈α∩handle␈α∩functional␈α∩arguments␈α∩so␈α∩it␈α∩is␈α∪not␈α∩too
␈↓suprising␈α
that␈α
all␈α
is␈α
not␈α
quite␈α
right.

␈↓The␈α␈↓αFUNARG␈↓␈αdevice␈αis␈αsufficient␈αfor␈αsimple␈αuses␈αof␈αfunctional␈αarguments␈αand␈αclosures.␈αHowever,
␈↓we␈αshould␈α
like␈αto␈α
return␈αfunctions␈α
and␈αclosures␈αas␈α
␈↓↓values␈↓.␈α Returning␈α
open␈αfunctions␈α
is␈αeasy;␈αwe␈α
can
␈↓simple␈α∞␈↓αcons␈↓-up␈α∂λ-expressions.␈α∞ Returning␈α∞closures␈α∂is␈α∞more␈α∞difficult;␈α∂Section␈α∞6.16␈α∞begins␈α∂to␈α∞discuss
␈↓this␈α
problem.

␈↓****more***

␈↓␈↓↓Problems␈↓

␈↓I␈α
Write␈α
complete␈α
versions␈α
of␈α
␈↓αeval␈↓␈α
and␈α
␈↓αapply␈↓.

________________
␈↓␈↓π 7␈↓␈αPerhaps␈αyou␈αcan␈αnow␈αsee␈α
why␈αquoted␈αexpressions,␈αconditional␈αexpressions,␈αand␈α␈↓αprog␈↓s␈α
are␈αcalled
␈↓␈↓↓special␈α
forms␈↓.
␈↓␈↓↓72  Running on the machine␈↓ B5.␈↓


␈↓␈↓ ¬}␈↓↓SECTION 5

␈↓↓␈↓ ∧sRUNNING ON THE MACHINE␈↓




␈↓This␈α
section␈α
is␈α
for␈α
the␈α
brave␈α
few␈α
who␈α
wish␈α
to␈α
run␈α
on␈α
a␈α
machine.␈α
 The␈α∞␈↓↓programming␈α
language␈↓,
␈↓LISP,␈α
is␈αthe␈α
Sexpr␈αtranslation␈α
of␈αthe␈α
LISP␈αfunctions␈α
and␈α␈↓αprog␈↓s␈α
that␈αwe␈α
have␈αbeen␈α
writing.␈αWhat
␈↓are␈α
some␈α
of␈α
the␈α
implications␈α
of␈α
writing␈α
in␈α
Sexpr␈α
form?

␈↓First,␈α
LISP␈α
becomes␈αthe␈α
world's␈α
largest␈α
parenthesis␈αsink.␈α
 It␈α
makes␈α
for␈αdifficult␈α
reading␈α
at␈αtimes,␈α
but
␈↓there␈αare␈αformatting␈αtricks,␈αand␈αediting␈αprograms␈αwhich␈αwill␈αhelp␈αthe␈αuser␈αmatch␈αparens␈αand␈α
locate
␈↓parts␈αof␈α
the␈αprogram.␈α
 (It␈αonly␈αhurts␈α
for␈αa␈α
little␈αwhile).␈α There␈α
is␈αa␈α
practical␈αbenefit␈α
which␈αgreatly
␈↓outweighs␈α
this␈α
anguish␈α
factor:␈α
since␈α
proper␈α
input␈α∞to␈α
LISP␈α
programs␈α
are␈α
Sexprs␈α
and␈α
since␈α∞we␈α
are
␈↓writing␈α
LISP␈α
programs␈α
in␈α∞Sexpr␈α
form␈α
then␈α
on␈α
input,␈α∞data␈α
and␈α
program␈α
are␈α∞indistinguishable␈α
in
␈↓format;␈αi.e.,␈αthe␈αare␈αboth␈αbinary␈αtrees.␈α
Obviously␈αfor␈αevaluation,␈αprograms␈αmust␈αhave␈αa␈αvery␈α
special
␈↓structure,␈αbut␈αprogram␈αand␈αdata␈αare␈αboth␈αtrees␈αjust␈αas␈αin␈αmore␈αhardware␈αmachines␈αthe␈αcontents␈αof
␈↓locations␈α⊗which␈α⊗contain␈α∃data␈α⊗are␈α⊗indistinguishable␈α⊗in␈α∃format␈α⊗from␈α⊗locations␈α⊗which␈α∃contain
␈↓instructions.

␈↓On␈α⊂the␈α⊂bare␈α∂machine␈α⊂it␈α⊂is␈α∂the␈α⊂way␈α⊂in␈α⊂which␈α∂a␈α⊂location␈α⊂is␈α∂accessed␈α⊂which␈α⊂determines␈α⊂how␈α∂the
␈↓contents␈α⊃of␈α⊃that␈α⊂location␈α⊃are␈α⊃interpreted.␈α⊃ If␈α⊂the␈α⊃central␈α⊃processor␈α⊂accesses␈α⊃the␈α⊃location␈α⊃via␈α⊂the
␈↓program␈αcounter,␈αthe␈αcontents␈αare␈αassumed␈αto␈α
be␈αan␈αinstruction.␈α That␈αsame␈αlocation␈αcan␈αusually␈α
be
␈↓accessed␈αas␈αpart␈αof␈αa␈α
data␈αmanipulating␈αinstruction.␈α In␈αthat␈α
case,␈αthe␈αcontents␈αare␈αsimply␈α
taken␈αto
␈↓be␈α
data.␈α∞ LISP␈α
is␈α∞one␈α
of␈α
the␈α∞few␈α
high-level␈α∞languages␈α
which␈α
also␈α∞has␈α
this␈α∞property.␈α
 It␈α∞gives␈α
the
␈↓programmer␈αexceptional␈αpower.␈α Let's␈αcomplete␈αthe␈αanalogy.␈α The␈αcentral␈αprocessor␈αhere␈αis␈αour␈αold
␈↓friend,␈α␈↓αeval␈↓.␈α If␈α␈↓αeval␈↓␈αreferences␈αa␈αbinary␈αtree␈αvia␈αits␈α`program␈αcounter',␈αthen␈αthat␈αtree␈αis␈αdecoded␈αvia
␈↓the␈α∞internals␈α∞of␈α∞␈↓αeval␈↓.␈α∞ If␈α∞a␈α∞tree␈α∞is␈α∂referenced␈α∞as␈α∞input␈α∞to␈α∞an␈α∞Sexpr-massaging␈α∞function,␈α∞then␈α∂it␈α∞is
␈↓taken␈αas␈αdata.␈α As␈αa␈αresult,␈αa␈αLISP␈αprogram␈αcan␈α␈↓αcons␈↓-up␈αa␈αnew␈αSexpr␈αwhich␈αcan␈αthen␈αbe␈α
evaluated
␈↓(i.e.,␈α
interpreted␈α
as␈α
an␈α
intruction).␈α
 The␈α
simplest␈α
way␈α
to␈α
communicate␈α
with␈α
such␈α
a␈α
machine␈α
is␈αto␈α
read
␈↓an␈αsexpr␈αtranslate␈αof␈αa␈αLISP␈αexpression␈αinto␈αmemory,␈αevaluate␈αthe␈αexpression,␈αand␈αprint␈αthe␈αresult.
␈↓Several␈α
implementations␈α
of␈α
LISP␈α
use␈α
a␈α
slight␈α
variant␈α
of␈α
this␈α
called␈α
the␈α
"read-eval-print"␈α
loop:

␈↓␈↓ ∧G␈↓αprog[[]  a  print[eval[read[];NIL]];go[a]]␈↓.

␈↓Since␈αprogramming␈αis␈αdone␈αusing␈αthe␈αsexpr␈αtranslation␈αof␈αLISP␈αfunctions␈αit␈αbecomes␈αconvenient␈αto
␈↓sometimes␈α
say␈α
"... the␈α
function␈α␈↓αCAR␈↓ ..."␈α
or␈α
"... write␈α
a␈α
␈↓αPROG␈↓ ...", ...␈αwhen␈α
we␈α
actually␈α
mean␈α
␈↓αcar␈↓␈αand
␈↓␈↓αprog␈↓, ....␈α∂ The␈α⊂actual␈α∂LISP␈α∂functions␈α⊂are␈α∂written␈α⊂in␈α∂the␈α∂language␈α⊂generated␈α∂by␈α⊂syntax␈α∂equations
␈↓which␈α∃we␈α∃have␈α⊗been␈α∃including.␈α∃This␈α⊗language␈α∃is␈α∃called␈α⊗the␈α∃meta-language,␈α∃and␈α⊗the␈α∃LISP
␈↓expressions␈α∂called␈α∂M-expressions␈α∂or␈α∂M-exprs.␈α∂ The␈α∂distinction␈α∂between␈α∂Mexprs␈α∂and␈α⊂their␈α∂Sexpr
␈↓translations␈α
must␈α
not␈α
be␈α
forgotten.

␈↓Though␈α␈↓αeval␈↓␈αis␈α
the␈αequivalent␈αof␈α
the␈αbasic␈αCpu␈αof␈α
the␈αLISP␈αmachine,␈α
there␈αis␈αanother␈α
artifact␈αin
␈↓many␈α⊃versions␈α∩of␈α⊃LISP␈α∩to␈α⊃communicate␈α∩with␈α⊃the␈α∩outside␈α⊃world.␈α∩ As␈α⊃with␈α∩most␈α⊃pieces␈α∩of␈α⊃I/O
␈↓equipment,␈α
it␈α
leaves␈α
something␈α
to␈α
be␈α
desired.␈α
 Its␈α
name␈α
is␈α
␈↓αevalquote␈↓.
␈↓␈↓↓5.1␈↓ 
⊗␈↓αevalquote␈↓↓     73␈↓α


␈↓␈↓ ¬|␈↓↓5.1  ␈↓αevalquote␈↓↓␈↓α


␈↓The␈α
basic␈α
structure␈α
of␈α
the␈α
␈↓αevalquote␈↓-loop␈α
is:

␈↓␈↓↓1.␈↓␈α
read␈α
in␈α
a␈α
(Sexpr␈α
representation␈α
of)␈α
function␈α
.␈α
I.e.,␈α
a␈α
name␈α
or␈α
a␈α
lambda␈α
expression.

␈↓␈↓↓2.␈↓␈α
read␈α
in␈α
a␈α
list␈α
of␈α
(evaluated)␈α
arguments␈α
to␈α
be␈α
applied␈α
to␈α
the␈α
function.

␈↓␈↓↓3.␈↓␈α
apply␈α
the␈α
function␈α
(of␈α
step␈α
␈↓↓1.␈↓)␈α
to␈α
the␈α
argument␈α
list.

␈↓␈↓↓4.␈↓␈α
print␈α
the␈α
value.

␈↓␈↓↓5.␈↓␈α
go␈α
to␈α
step␈α
␈↓↓1.␈↓

␈↓Thus␈α
the␈α
structure␈α
of␈α
the␈α
loop␈α
is␈α
essentially:
␈↓α                   prog[[fn;x;z]
␈↓α␈↓ ∧8l␈↓ ∧hfn ← read[ ];
␈↓α␈↓ ∧8␈↓ ∧hx ← read[ ];
␈↓α␈↓ ∧8␈↓ ∧hz ← evalquote[fn;x];
␈↓α␈↓ ∧8␈↓ ∧hprint[z];
␈↓α␈↓ ∧8␈↓ ∧hgo[l]]]

␈↓α␈↓where: ␈↓αevalquote <= λ[[fn;x]apply[fn;x;NIL]]

␈↓or more concisely:

␈↓α␈↓ ∧8prog[[ ]
␈↓α␈↓ ∧8 l    print[evalquote[read[ ];read[ ]]];
␈↓α␈↓ ∧8      go[l]]


␈↓where:

␈↓␈↓↓1.␈↓␈α
the␈α
function,␈α
␈↓αread␈↓,␈α
negotiates␈α
with␈α
an␈α
input␈α
device␈α
to␈α
read␈α
an␈α
Sexpression.

␈↓␈↓↓2.␈↓␈α
the␈α
function,␈α
␈↓αprint␈↓,␈α
prints␈α
the␈α
value␈α
of␈α
its␈α
argument␈α
on␈α
an␈α
output␈α
device.

␈↓The␈α
details␈α
of␈α
␈↓αread␈↓␈α
and␈α
␈↓αprint␈↓␈α
will␈α
appear␈α
when␈α
we␈α
discuss␈α
implementation␈α
of␈α
LISP.

␈↓Here's␈α↔an␈α_example␈α↔of␈α_the␈α↔behavior␈α_of␈α↔␈↓αevalquote␈↓.␈α_Assume␈α↔that␈α_the␈α↔input␈α_device␈α↔contains
␈↓␈↓↓CAR ((A B))␈↓.␈α
Then␈α
the␈αfirst␈α
␈↓αread␈↓␈α
in␈α␈↓αevalquote␈↓␈α
gets␈α
␈↓↓CAR␈↓␈α(a␈α
string␈α
representing␈αan␈α
Sexpr),␈α
and␈αthe
␈↓second␈α
read␈α
gets␈α
␈↓↓((A B))␈↓;␈α
then␈α
␈↓αapply␈↓␈α
gets␈α
called␈α
as:␈α
␈↓α

␈↓α␈↓ ¬ apply[CAR;((A B));NIL].  ␈↓ 
␈↓␈↓↓74  Running on the machine␈↓ 65.1␈↓


␈↓␈↓αapply␈↓␈αsays␈α
that␈αthis␈αis␈α
the␈αsame␈α
as␈αevaluating␈α␈↓αcar[(A B)]␈↓,␈α
and␈αthus␈α
returns␈α␈↓αA␈↓␈αas␈α
its␈αanswer,␈αwhich␈α
is
␈↓dutifully␈α
printed␈α
by␈α
␈↓αprint␈↓.

␈↓So␈α∂␈↓αevalquote␈↓␈α⊂can␈α∂be␈α∂used␈α⊂as␈α∂a␈α∂`desk␈α⊂calculator'␈α∂for␈α∂LISP.␈α⊂If␈α∂we␈α∂wish␈α⊂to␈α∂evaluate␈α⊂an␈α∂expression
␈↓␈↓αf[a␈↓β1␈↓α; ... a␈↓βn␈↓α]␈↓␈α∩for␈α∩␈↓αa␈↓βi␈↓␈α∩constants,␈α∩i.e.␈α∩sexprs,␈α∩then␈α⊃␈↓αevalquote[F; (a␈↓β1␈↓α ... a␈↓βn␈↓α)]␈↓␈α∩will␈α∩do␈α∩it␈α∩for␈α∩us.␈α∩ But␈α⊃the
␈↓␈↓αevalquote␈↓-loop␈αwill␈α␈↓↓not␈↓␈αevaluate␈αarguments;␈αthe␈αa␈↓βi␈↓'s␈αin␈αthe␈αcall␈αon␈α␈↓αevalquote␈↓␈αare␈αnot␈αexpressions,␈αnot
␈↓translates␈α⊂of␈α⊂constants,␈α⊂but␈α⊂are␈α⊂constants.␈α⊂ Typing␈α⊂␈↓↓CONS ((CAR (QUOTE (A))) (QUOTE (B)))␈↓␈α∂to
␈↓the␈α
␈↓αevalquote␈↓-loop␈α
will␈α
result␈α
in␈α
␈↓α((CAR (QUOTE (A))) . (QUOTE (B)))␈↓.

␈↓The␈α␈↓αevalquote␈↓-loop␈α
is␈αan␈α
anomaly.␈αIt␈α
does␈αnot␈αexpect␈α
the␈αusual␈α
representation␈αof␈α
a␈αLISP␈α
form␈αsay
␈↓␈↓α(F␈α
e␈↓β1␈↓α␈α
...␈αe␈↓βn␈↓α)␈↓␈α
where␈α
the␈α
␈↓αe␈↓βi␈↓'s␈αare␈α
to␈α
be␈α
evaluated.␈αIt␈α
expects␈α
a␈α
␈↓↓pair␈↓␈αof␈α
sexprs␈α
representing␈α
a␈αfunction
␈↓and␈α∪␈↓↓evaluated␈↓␈α∪arguments.␈α∪The␈α∪benefits␈α∀of␈α∪having␈α∪"functional-notation"␈α∪as␈α∪input␈α∀and␈α∪having
␈↓arguments␈αimplicitly␈αevaluated␈α
are␈αgreatly␈αoutweighed␈αby␈α
the␈αconfusion␈αintroduced␈αby␈α
having␈αtwo
␈↓formats␈α
for␈α
LISP␈α
expressions,␈α
one␈α
for␈α
the␈α
"top-level"␈α
and␈α
a␈α
different␈α
one␈α
everywhere␈α
else.

␈↓Certainly␈α
before␈α
we␈α
can␈α
do␈α
any␈α
reasonable␈α
amount␈α
of␈α
calculation,␈α
we␈α
must␈α
be␈α
able␈α
to␈α
define␈αand
␈↓name␈αour␈αown␈αfunctions.␈αThe␈α␈↓αlabel␈↓␈αoperator,␈αor␈αcalling␈α␈↓αeval␈↓␈αwith␈αan␈αintial␈αsymbol␈αtable␈αcontaining
␈↓our␈α∞defintions␈α∞will␈α∞suffice,␈α∞but␈α∞this␈α∞is␈α∞not␈α∞particularly␈α∞elegant.␈α∞ We␈α∞would␈α∞like␈α∞our␈α∞definitions␈α∞to
␈↓have␈α
some␈α
permanence.

␈↓A␈αfunction␈α(actually␈αa␈α␈↓↓special␈αform␈↓,␈αif␈αyou␈αhave␈αbeen␈αpaying␈αattention)␈αnamed␈α␈↓αdefine␈↓,␈αwhose␈αeffect
␈↓is␈αto␈αadd␈αdefinitions␈αto␈α␈↓αapply␈↓,␈αhas␈αbeen␈α
provided.␈αThe␈αactual␈αbehavior␈αof␈α␈↓αdefine␈↓␈αwill␈αappear␈αin␈α
the
␈↓sections␈α
on␈α
implementation.

␈↓␈↓αdefine␈↓␈α
is␈α
the␈αname␈α
of␈α
a␈αspecial␈α
form␈α
(its␈αarguments␈α
are␈α
not␈α
evaluated)␈αof␈α
one␈α
argument,␈αsay␈α
␈↓αx.␈α
x␈α␈↓is␈α
a
␈↓list␈αof␈αpairs:␈α␈↓α((u␈↓β1␈↓α␈αv␈↓β1␈↓α)␈α...␈α(u␈↓βn␈↓α␈αv␈↓βn␈↓α)).␈↓␈αEach␈α␈↓αu␈↓βi␈↓␈αis␈αthe␈αname␈αof␈αa␈αfunction␈αand␈αeach␈α␈↓αv␈↓βi␈↓␈αis␈α
the␈αλ-expression
␈↓definition.␈α Actually␈αeach␈α␈↓αu␈↓βi␈↓␈αand␈α␈↓αv␈↓βi␈↓␈αis␈αthe␈α␈↓↓Sexpr␈αtranslation␈↓␈αof␈αthe␈αname␈αand␈αthe␈αfunction,␈αbut␈αyou
␈↓knew␈α
that␈α
anyway.␈α
 The␈α
effect␈α
of␈α
␈↓αdefine␈↓␈α
is␈α
to␈α
put␈α
the␈α
appropriate␈α
definitions␈α
in␈α
the␈α
symbol␈α
table.

␈↓For␈α
example␈α
we␈α
might␈α
wish␈α
to␈α
define␈α
the␈α
function␈α
which␈α
reverses␈α
a␈α
list␈α
as:␈α
␈↓α

␈↓αreverse <= λ[[x] prog[[y;z]
␈↓α                       y ← x;
␈↓α                       z ← NIL;
␈↓α                     a [null[y] → return [z]];
␈↓α                       z ← cons[car[y];z];
␈↓α                       y ← cdr[y];
␈↓α                       go[a];]]

␈↓Then the following would make this definition grist for the ␈↓αevalquote␈↓ mill.
␈↓␈↓↓5.1␈↓ 
⊗␈↓αevalquote␈↓↓     75␈↓α



␈↓αDEFINE((
␈↓α        (REVERSE (LAMBDA (X)
␈↓α                         (PROG (Y Z)
␈↓α                           (SETQ Y X)
␈↓α                           (SETQ Z NIL)
␈↓α                         A(COND ((NULL Y)(RETURN Z)))
␈↓α                           (SETQ Z (CONS (CAR Y) Z))
␈↓α                           (SETQ Y (CDR Y))
␈↓α                           (GO A) )))
␈↓α                                       ))

␈↓and subsequently:

␈↓␈↓αREVERSE ((A B C))  ␈↓would evaluate to: ␈↓α(C B A).␈↓


␈↓α␈↓C.␈αWeissman's␈αLISP␈α
PRIMER␈αis␈αan␈αexcellent␈α
source␈αof␈αmachine␈α
examples␈αand␈αshould␈αbe␈α
consulted
␈↓now.␈α∩Always␈α∩remember␈α∩that␈α∩you␈α∩are␈α∪writing␈α∩the␈α∩sexpr␈α∩representation␈α∩of␈α∩LISP␈α∪functions␈α∩and
␈↓expressions.␈α
Do␈α
not␈α
confuse␈α
the␈α
two.



␈↓␈↓ ∧u␈↓↓5.2  A project: extensions to ␈↓αeval␈↓↓␈↓α


␈↓***h.s.␈α
about␈α
how␈α
wonderful␈α
LISP␈α
is␈α
for␈α
extenson***

␈↓Consider␈αa␈αp␈↓βi␈↓␈α
→␈αe␈↓βi␈↓␈αcomponent␈α
of␈αa␈αconditional␈α
expression.␈α As␈αconditionals␈α
are␈αnow␈αdefined,␈αe␈↓βi␈↓␈α
must
␈↓be␈αa␈αsingle␈αexpression␈αto␈αbe␈αevaluated.␈α In␈αsubsets␈αof␈αLISP␈αwithout␈αside-effects␈αthis␈αis␈αsufficient.␈αIt
␈↓is,␈αhowever,␈αconvenient␈αin␈αpractice,␈αi.e.,␈αin␈αLISP␈α␈↓↓with␈↓␈αside␈αeffects,␈αto␈αextend␈αconditionals␈αto␈αinclude
␈↓components␈α
of␈α
the␈αform:␈α
p␈↓βi␈↓ → e␈↓βi1␈↓;␈α
...␈αe␈↓βin␈↓.␈α
 This␈α
extended␈α
component␈αis␈α
to␈α
be␈αevaluated␈α
as␈α
follows:␈αif␈α
p␈↓βi␈↓
␈↓is␈αtrue,␈αthen␈αevaluate␈αthe␈α
e␈↓βij␈↓'s␈αfrom␈αleft␈αto␈αright,␈α
with␈αthe␈αvalue␈αof␈αthe␈α
component␈αto␈αbe␈αthe␈αvalue␈α
of
␈↓e␈↓βin␈↓.

␈↓For␈α
example,␈α
this␈α
feature,␈α
used␈α
in␈α
␈↓αprog␈↓s␈α
would␈α
allow␈α
us␈α
to␈α
replace:

␈↓␈↓ αX␈↓ β_ ....
␈↓␈↓ αX␈↓ β_[p␈↓β1␈↓ → ␈↓αgo[l]␈↓]
␈↓␈↓ αX␈↓ β_ ...
␈↓␈↓ αX␈↓αl␈↓ β_␈↓e␈↓β1␈↓;
␈↓␈↓ αX␈↓ β_e␈↓β2␈↓;
␈↓␈↓ αX␈↓ β_ ...
␈↓␈↓ αX␈↓ β_␈↓αgo[m];
␈↓␈↓↓76  Running on the machine␈↓ 45.2␈↓


␈↓with:␈α
[p␈↓β1␈↓␈α→␈α
e␈↓β1␈↓;e␈↓β2␈↓;␈α
...␈α␈↓αgo[m]␈↓;].␈α
This␈αis␈α
certainly␈α
easier␈αto␈α
read.␈α
 This␈αextended␈α
conditional␈αexpression␈α
is
␈↓available␈α
on␈α
versions␈α
of␈α
LISP␈α
1.6␈α
on␈α
the␈α
PDP-10.

␈↓Here␈α∀is␈α∀another␈α∀cosmetic.␈α∀Instead␈α∀of␈α∀requiring␈α∀that␈α∀the␈α∀body␈α∀of␈α∀a␈α∀λ-definition␈α∀be␈α∀a␈α∪single
␈↓expression:␈α␈↓αλ[[ ... ]f[ ... ]]␈↓,␈αallow␈α
bodies␈αof␈αthe␈αform:␈α
␈↓αλ[[ ... ]f␈↓β1␈↓α[ ... ]; ... f␈↓βn␈↓α[ ... ]]␈↓.␈αThe␈αevaluation␈αof␈α
such
␈↓should␈α
mean;␈α
bind␈α
as␈α
usual,␈α
evaluate␈α
the␈α
␈↓αf␈↓βi␈↓'s␈α
from␈α
left␈α
to␈α
right,␈α
returning␈α
as␈α
value␈α
␈↓αf␈↓βn␈↓α[ ... ]␈↓.

␈↓This␈α∀next␈α∪extension␈α∀to␈α∪␈↓αeval␈↓␈α∀was␈α∪derived␈α∀from␈α∪the␈α∀syntax␈α∪of␈α∀MUDDLE,␈α∀CONNIVER,␈α∪and
␈↓MICRO-PLANNER.␈α∂ We␈α∂have␈α∂seen␈α∂that␈α∂LISP␈α∞calling␈α∂sequences␈α∂are␈α∂of␈α∂two␈α∂varieties:␈α∞functions
␈↓calls,␈α
evaluating␈α
␈↓↓all␈↓␈α
of␈α
the␈α
arguments;␈α
and␈α
special␈α
forms,␈α
evaluating␈α
␈↓↓none␈↓␈α
of␈α
the␈α
arguments.

␈↓In␈α∞an␈α
attempt␈α∞to␈α
generalize␈α∞this␈α
regime␈α∞we␈α
might␈α∞allow␈α
the␈α∞evaluation␈α
of␈α∞some␈α
of␈α∞the␈α
arguments
␈↓and␈α⊂enlarge␈α⊂on␈α⊂the␈α⊂domain␈α⊂of␈α⊂objects␈α⊂which␈α∂can␈α⊂appear␈α⊂in␈α⊂the␈α⊂list␈α⊂of␈α⊂λ-variables.␈α⊂ We␈α∂might
␈↓partition␈α⊃the␈α⊂formal␈α⊃parameters␈α⊂into␈α⊃obligatory␈α⊂parameters,␈α⊃optional␈α⊂parameters,␈α⊃and␈α⊃an␈α⊂excess
␈↓collector.␈α∀ Obligatory␈α∀parameters␈α∀␈↓↓must␈↓␈α∀have␈α∀corresponding␈α∀actual␈α∀parameters;␈α∀optional␈α∪actual
␈↓parameters␈α
are␈α
used␈α
if␈α∞present,␈α
otherwise␈α
declared␈α
default␈α
values␈α∞are␈α
used;␈α
and␈α
if␈α
there␈α∞are␈α
more
␈↓actual␈α
parameters␈α
than␈α∞the␈α
formals␈α
encompassed␈α∞by␈α
the␈α
first␈α
two␈α∞classes,␈α
then␈α
they␈α∞are␈α
associated
␈↓with␈α
the␈α
excess␈α
collector.

␈↓To␈α
be␈α
more␈α
precise␈α
consider␈α
the␈α
following␈α
possible␈α
BNF␈α
equations:

␈↓<varlist>␈↓ βλ::=[<obligatory> <optional> <excess>]

␈↓<obligatory>␈↓ βλ::= <par>; ...<par> | ␈↓ε␈↓

␈↓<optional>␈↓ βλ::= "optional" <opn>; ... <opn> | ␈↓ε␈↓

␈↓<excess>␈↓ βλ::= "excess" <par> | ␈↓ε␈↓

␈↓<par>␈↓ βλ::= <variable> | @<variable>

␈↓<opn>␈↓ βλ::= <par> | <par> ← <form>


␈↓The␈α
associated␈α
semantics␈α
follows:

␈↓␈↓↓1.␈↓␈α
The␈α
formal␈α
parameters␈α
are␈α
to␈α
be␈α
bound␈α
to␈α
the␈α
actual␈α
parameters␈α
from␈α
left␈α
to␈α
right␈α
(as␈α
usual).

␈↓␈↓↓2.␈↓␈α∂There␈α∂must␈α∂be␈α∂an␈α⊂actual␈α∂parameter␈α∂for␈α∂each␈α∂obligatory␈α⊂parameter,␈α∂and␈α∂if␈α∂there␈α∂is␈α⊂no␈α∂excess
␈↓␈↓ αhcollector␈αthere␈αmay␈αnot␈αbe␈αmore␈αactual␈αparameters␈αthan␈αformals.␈α(There␈αmay␈αbe␈αless␈αif
␈↓␈↓ αhwe␈α
have␈α
optionals.)

␈↓␈↓↓3.␈↓␈α⊂If␈α⊂a␈α⊂<variable>␈α∂in␈α⊂a␈α⊂formal␈α⊂parameter␈α∂is␈α⊂preceeded␈α⊂by␈α⊂a␈α∂"@",␈α⊂then␈α⊂the␈α⊂corresponding␈α∂actual
␈↓␈↓ αhparameter␈α
is␈α
␈↓↓not␈↓␈α
evaluated.
␈↓␈↓↓5.2␈↓ λA project: extensions to ␈↓αeval␈↓↓     77␈↓α


␈↓␈↓↓4.␈↓␈αWe␈αmight␈αrun␈αout␈αof␈αactual␈αparameters␈α
while␈αbinding␈αthe␈αoptionals.␈α If␈αwe␈αdo,␈αthen␈αwe␈α
look␈αat
␈↓␈↓ αhthe␈α
remaining␈α
formal␈α
optionals.␈α
 If␈αa␈α
formal␈α
is␈α
simply␈α
a␈α
<par>␈αthen␈α
bind␈α
it␈α
to␈α
␈↓αNIL␈↓;␈αif␈α
a
␈↓␈↓ αhformal␈α⊂is␈α∂@<variable>␈α⊂←␈α⊂<form>␈α∂then␈α⊂bind␈α⊂the␈α∂<variable>␈α⊂to␈α⊂the␈α∂<form>;␈α⊂or␈α⊂if␈α∂the
␈↓␈↓ αhformal␈α
is␈α
<variable>␈α
←␈α
<form>,␈α
bind␈α
<variable>␈α
to␈α
the␈α
value␈α
of␈α
<form>.

␈↓␈↓↓5.␈↓␈αFinally,␈αthe␈αexcess␈αcollector␈αis␈αbound␈αto␈αa␈αlist␈αof␈αany␈αremaining␈αactual␈αparameters␈αin␈αthe␈αobvious
␈↓␈↓ αhway:␈α∞if␈α∂<par>␈α∞is␈α∂<variable>␈α∞then␈α∂form␈α∞a␈α∂list␈α∞of␈α∂the␈α∞values␈α∂of␈α∞the␈α∂remainder;␈α∞if␈α∂it␈α∞is
␈↓␈↓ αh@<variable>,␈α
bind␈α
<variable>␈α
to␈α
the␈α
actual␈α
list.␈α
If␈α
there␈α
is␈α
no␈α
excess,␈α
bind␈α
to␈α
␈↓αNIL␈↓.

␈↓These␈α∪same␈α∀languages␈α∪have␈α∀extended␈α∪␈↓αprog␈↓-variables␈α∀slightly,␈α∪allowing␈α∀them␈α∪to␈α∀be␈α∪initialized
␈↓explicitly.␈α∃If␈α∃a␈α∃␈↓αprog␈↓-variable␈α∃is␈α∃atomic,␈α∃intialize␈α∃it␈α∃to␈α∃␈↓αNIL␈↓,␈α∃as␈α∃usual.␈α∃If␈α∃it␈α∃is␈α∃of␈α∃the␈α∃form
␈↓<variable> ← <form>␈α
then␈α
initialize␈α
it␈α
to␈α
the␈α
value␈α
of␈α
the␈α
<form>.

␈↓Here␈α
are␈α
some␈α
examples:

␈↓1.␈α
In␈α
the␈α
initialization␈α
of␈α
␈↓αlength␈↓␈α
on␈α
page␈α
67,␈α
we␈α
could␈α
write:␈α
␈↓α... prog[[l ← x; c ← 0] ....␈↓

␈↓2.␈α
␈↓αlist␈↓␈α
could␈α
now␈α
be␈α
defined␈α
as:␈α
␈↓αλ[[␈↓"excess"␈↓α␈α
x]x]␈↓.

␈↓3.␈α
Consider␈α
the␈α
following␈α
definition:

␈↓␈↓ αX␈↓αbaz <= λ[␈↓ βx[x;␈↓@␈↓αy;␈↓"optional"␈↓α z; u ← 0; ␈↓"excess"␈↓α v]
␈↓α␈↓ αX␈↓ βxprint[x];
␈↓α␈↓ αX␈↓ βxprint[y];
␈↓α␈↓ αX␈↓ βxprint[z];
␈↓α␈↓ αX␈↓ βxprint[u];
␈↓α␈↓ αX␈↓ βxprint[v]].
␈↓α␈↓Then a call of:
␈↓␈↓αeval[(BAZ 2 (CAR (QUOTE (X Y)) 4 5 6 7 (CAR (QUOTE (A . B))));NIL]␈↓ would print:

␈↓α2
␈↓α(CAR(QUOTE (X Y))
␈↓α4
␈↓α5
␈↓α(6 7 A)␈↓
␈↓(and return value: ␈↓α(6 7 A)␈↓.

␈↓Similarly defining;
␈↓␈↓↓78  Running on the machine␈↓ 45.2␈↓


␈↓␈↓ αX␈↓αfii <= λ[␈↓ βx[x;y;␈↓"optional"␈↓α z; u ← 0; ␈↓"excess"␈↓α v]
␈↓α␈↓ αX␈↓ βxprint[x];
␈↓α␈↓ αX␈↓ βxprint[y];
␈↓α␈↓ αX␈↓ βxprint[z];
␈↓α␈↓ αX␈↓ βxprint[u];
␈↓α␈↓ αX␈↓ βxprint[v]].

␈↓α␈↓and calling:
␈↓␈↓αeval[(FII 2 (CAR (QUOTE (X Y)));NIL]␈↓ prints:
␈↓α2
␈↓αX
␈↓αNIL
␈↓α0
␈↓αNIL.␈↓

␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓Design␈α∞simple␈α∞sexpr␈α∞representations␈α∞of␈α∞these␈α
proposed␈α∞constructs.␈α∞ Make␈α∞these␈α∞extensions␈α∞to␈α
␈↓αeval␈↓.
␈↓How␈α
useful␈α
are␈α
these␈α
syntactic␈α
sugarings?



␈↓␈↓ ∧A␈↓↓5.3  A project: Syntax-directed processes␈↓


␈↓We␈α∀should␈α∀now␈α∪have␈α∀sufficient␈α∀background␈α∀to␈α∪examine␈α∀reasonably␈α∀complex␈α∀and␈α∪non-trivial
␈↓problems.␈α∪ This␈α∩project␈α∪is␈α∩an␈α∪introduction␈α∩to␈α∪the␈α∩very␈α∪important␈α∩area␈α∪called␈α∩␈↓↓syntax-directed
␈↓↓processes␈↓.␈α⊂ Syntax-directed␈α∂techniques␈α⊂are␈α∂used␈α⊂extensively␈α∂in␈α⊂compiler␈α∂construction␈α⊂(see␈α∂Section
␈↓7.9).␈α∞We␈α∂shall␈α∞begin␈α∞by␈α∂applying␈α∞these␈α∞techniques␈α∂to␈α∞evaluation.

␈↓As␈αwe␈αsaid␈αearlier␈αthere␈αare␈αalternatives␈αto␈αthe␈αcall-by-value␈αevaluation␈αscheme;␈αand␈αas␈αwe␈αall␈α
know
␈↓there␈α
are␈α
alternatives␈α
to␈α
the␈α
prefix␈α
notation␈α
which␈α
we␈α
chose␈α
to␈α
represent␈α
function␈α
application.

␈↓For␈αexample,␈αin␈αgrade␈αschool␈αwe␈αall␈α
learned␈αinfix␈αnotation␈αand␈αits␈αimplied␈αprecedence␈αrelations,␈α
say
␈↓for␈α+␈αand␈α*.␈αSimply␈αbecause␈αinfix␈αnotation␈αis␈αthe␈αfirst␈αrepresentation␈αwe␈αsee␈αdoesn't␈αmean␈αthat␈αit␈αis
␈↓the␈α
most␈α
convenient␈α
for␈α
evaluation␈α
either␈α
by␈α
us␈α
or␈α
by␈α
machine.

␈↓Let's␈αtake␈αas␈αexample␈αthe␈αexpression:␈α2+3*5.␈α The␈αgrade␈αschool␈αprecedence␈αrelations␈αsay␈αthat␈α*␈α
takes
␈↓precedence␈α∂over␈α∂+.␈α∂ That␈α∂is␈α∂the␈α∂expression␈α∂represents:␈α∂2+(3*5)␈α∂rather␈α∂than␈α∂(2+3)*5.␈α∂ We␈α∂can␈α∂also
␈↓easily␈α
write␈α
the␈α
expression␈α
in␈α
prefix␈αnotation:␈α
+[2;*[3;5]].␈α
 Here␈α
the␈α
precedence␈α
of␈α
operations␈αis␈α
made
␈↓explicit.␈α
Similar␈αpostfix␈α
notation␈α(where␈α
the␈αoperators␈α
follow␈αrather␈α
than␈αprecede␈α
the␈α
operands)␈αis
␈↓easy:␈α
[2;[3;5]*]+.

␈↓Why␈α↔this␈α↔preoccupation␈α_with␈α↔notation?␈α↔Well,␈α↔some␈α_notational␈α↔schemes␈α↔lend␈α_themselves␈α↔to
␈↓mechanical␈αevaluation␈αbetter␈αthan␈αothers.␈α There␈αis␈αa␈αgreat␈αdeal␈αof␈αimplied␈αintelligence␈αrequired␈αin
␈↓␈↓↓5.3␈↓ π"A project: Syntax-directed processes     79␈↓


␈↓the␈α
usual␈α∞infix␈α
scheme.␈α
 We␈α∞have␈α
already␈α
seen␈α∞one␈α
very␈α
mechanical␈α∞method␈α
for␈α∞evaluating␈α
some
␈↓prefix␈α
expressions:␈α
the␈α
␈↓αvalue␈↓␈α
function␈α
in␈α
Section␈α
3.7.

␈↓Consider␈αthe␈αpostfix␈αnotation.␈αWe␈αwill␈αfirst␈αclaim␈αthat␈αsince␈αwe␈αknow␈αthat␈αplus␈αand␈αtimes␈αare␈αboth
␈↓binary␈αoperations,␈αthe␈αpunctuation,␈α ], [,␈αand␈α; , ␈αis␈αredundant␈α(this␈αis␈αalso␈αtrue␈αfor␈αprefix␈αnotation).
␈↓Thus␈α
the␈α
string,␈α
2 3 5 * +,␈α
contains␈α
the␈α
same␈α
information␈α
as␈α
[2;[3;5]*]+.

␈↓A␈αstrong␈αpoint␈αof␈αpostfix␈αstring␈αnotation␈αis␈αits␈αease␈αof␈αevaluation.␈α Using␈α"↓"␈αto␈αpoint␈αto␈αthe␈α
current
␈↓position␈αin␈αthe␈αstring␈αand␈αusing␈αthe␈α"| ... |"-notation␈αof␈αpage␈α54␈αto␈αrepresent␈αthe␈αstack,␈αthe␈αfollowing
␈↓is␈α
a␈α
trace␈α
of␈α
the␈α
evaluation␈α
of␈α
the␈α
above␈α
string␈α
,␈α
2 3 5 * +.

␈↓↓                       ↓                   ↓
␈↓2 3 5 * + ,|   |  =>  3 5 * + ,| 2 |  => 5 * + , | 3 |  =>
␈↓                                                      | 2 |


␈↓↓                ↓
␈↓* + , | 5 |  =>  + , | 15 |  => | 17 |.
␈↓      | 3 |           |  2 |
␈↓      | 2 |


␈↓It␈α∞is␈α∞a␈α∞very␈α∞simple␈α∞task␈α∞to␈α∞program␈α∞this␈α∞scheme␈α∞as␈α∞a␈α∞LISP␈α∞␈↓αprog␈↓.␈α∞ Similarly,␈α∞it␈α∞is␈α∞quite␈α∞simple␈α∞to
␈↓extend␈α
this␈α
evaluation␈α
scheme␈α
to␈α
n-ary␈α
operators.

␈↓Given␈α∞an␈α∂arbitrary␈α∞arithmetic␈α∂expression␈α∞involving␈α∞constants,␈α∂and␈α∞the␈α∂binary␈α∞operations␈α∂of␈α∞plus
␈↓and␈α
times,␈α
we␈αhave␈α
a␈α
straightforward␈α
mechanical␈αevaluation␈α
scheme.␈α
It␈αis␈α
intuitively␈α
clear␈α
how␈αto
␈↓translate␈αinfix␈αexpressions␈αinto␈α
postfix␈αnotation.␈α If␈αwe␈αcould␈α
mechanize␈αthis␈αprocess␈αthen␈αwe␈α
would
␈↓have␈αan␈α
algorithm␈αfor␈αthe␈α
evaluation␈αof␈αinfix␈α
expressions.␈α First␈αlet's␈α
attempt␈αto␈α
describe␈αprecisely
␈↓the␈α∂class␈α∂of␈α∂infix␈α∂expressions␈α∂which␈α∂we␈α∂wish␈α∂to␈α∂evaluate.␈α∂The␈α∂BNF␈α∂notation␈α∂is␈α∂a␈α∂good␈α∞vehicle.
␈↓Perhaps␈α
the␈α
following:

␈↓␈↓ βx<exp>␈↓ ∧x::= <exp><binop><exp>
␈↓␈↓ βx␈↓ ∧x::= <integer>

␈↓␈↓ βx<binop>␈↓ ∧x::= + | *    .

␈↓There␈αare␈αmany␈αthings␈αwrong␈αwith␈αthis␈αattempt␈αat␈αa␈αgrammar.␈α First,␈αmany␈αexpressions␈αhave␈αmore
␈↓than␈α∞one␈α∂possible␈α∞description␈α∂or␈α∞parse␈α∂tree␈α∞(the␈α∂grammar␈α∞is␈α∂said␈α∞to␈α∂be␈α∞ambiguous).␈α∂Second,␈α∞this
␈↓grammar␈α
doesn't␈α
express␈α
our␈α
usual␈α
precedence␈α
relations.␈α
The␈α
next␈α
attempt␈α
is␈α
successful:
␈↓␈↓↓80  Running on the machine␈↓ 45.3␈↓



␈↓␈↓ βx<exp>␈↓ ¬(::= <exp> + <term>␈↓ λH(1)
␈↓␈↓ βx␈↓ ¬(::= <term>␈↓ λH(2)

␈↓␈↓ βx<term>␈↓ ¬(::= <term> * <factor>␈↓ λH(3)
␈↓␈↓ βx␈↓ ¬(::= <factor>␈↓ λH(4)

␈↓␈↓ βx<factor>␈↓ ¬(::= ( <exp> )␈↓ λH(5)
␈↓␈↓ βx␈↓ ¬(::= <integer>␈↓ λH(6)
␈↓␈↓ βx<integer>␈↓ ¬(::= 0, 1, 2, ....␈↓ λH(7)


␈↓For␈α
example␈α
the␈α
(only)␈α
parsing␈α
of␈α
2+3*5␈α
is:
␈↓                   <exp>
␈↓                     |
␈↓             <exp>  +  <term>
␈↓              |               |
␈↓           <term>     <term>*<factor>
␈↓              |             |           |
␈↓           <factor>  <factor>  <integer>
␈↓               |            |               |
␈↓           <integer>  <integer>      5
␈↓               |           |
␈↓               2          3



␈↓Our␈α∂next␈α⊂step␈α∂will␈α∂require␈α⊂a␈α∂temporary␈α⊂act␈α∂of␈α∂faith.␈α⊂ We␈α∂will␈α⊂show␈α∂later␈α∂that␈α⊂this␈α∂faith␈α⊂is␈α∂not
␈↓ill-placed.

␈↓Assumption:␈α
Given␈αan␈α
arbitrary␈α
(well-formed)␈αarithmetic␈α
expression,␈αe,␈α
of␈α
our␈αabove␈α
class,␈α
we␈αcan
␈↓␈↓ αhfind␈αthe␈α
left-most␈αwell-formed␈α
subexpression,␈αs,␈α
such␈αthat␈αs␈α
is␈αan␈α
instance␈αof␈α
the␈αRHS
␈↓␈↓ αhof␈α
one␈α
of␈αthe␈α
rules,␈α
(1)-(7).␈αLet␈α
e'␈α
be␈αthe␈α
expression␈α
obtained␈αfrom␈α
e,␈α
by␈α
replacing␈αthe
␈↓␈↓ αhoccurrence␈α
of␈α
the␈α
RHS␈α
by␈α
the␈α
LHS,␈α
then␈α
our␈α
assumption␈α
is␈α
also␈α
applicable␈α
to␈α
e'.
␈↓␈↓↓5.3␈↓ π"A project: Syntax-directed processes     81␈↓


␈↓For example,

␈↓␈↓ αλe␈↓ ∧hs␈↓ πλe'␈↓ 	Xrule

␈↓␈↓ αλ  2+3*5␈↓ ∧h  2␈↓ πλ<integer>+3*5␈↓ 	X(7)
␈↓␈↓ αλ<integer>+3*5␈↓ ∧h<integer>␈↓ πλ<factor>+3*5␈↓ 	X(6)
␈↓␈↓ αλ<factor>+3*5␈↓ ∧h<factor>␈↓ πλ<term>+3*5␈↓ 	X(4)
␈↓␈↓ αλ<term>+3*5␈↓ ∧h<term>␈↓ πλ<exp>+3*5␈↓ 	X(2)
␈↓␈↓ αλ<exp>+3*5␈↓ ∧h3␈↓ πλ<exp>+<integer>*5␈↓ 	X(7)
␈↓␈↓ αλ<exp>+<integer>␈↓ ∧h<integer>␈↓ πλ<exp>+<factor>*5␈↓ 	X(6)
␈↓␈↓ αλ<exp>+<factor>*5␈↓ ∧h<factor>␈↓ πλ<exp>+<term>*5␈↓ 	X(4)
␈↓␈↓ αλ<exp>+<term>*5␈↓ ∧h5␈↓ πλ<exp>+<term>*<integer>␈↓ 	X(7)
␈↓␈↓ αλ<exp>+<term>*<integer>␈↓ ∧h<integer>␈↓ πλ<exp>+<term>*<factor>␈↓ 	X(6)
␈↓␈↓ αλ<exp>+<term>*<factor>␈↓ ∧h<term>*<factor>␈↓ πλ<exp>+<term>␈↓ 	X(3)
␈↓␈↓ αλ<exp>+<term>␈↓ ∧h<exp>+<term>␈↓ πλ<exp>␈↓ 	X(1)


␈↓Let␈α
us␈α
now␈αassociate␈α
an␈α
action␈αwith␈α
each␈α
of␈αthe␈α
rules␈α
(1)-(7)␈αsuch␈α
that␈α
whenever␈αwe␈α
apply␈α
one␈αof
␈↓the␈α
rule␈α
in␈α
the␈α
above␈α
reduction␈α
technique,␈αwe␈α
will␈α
also␈α
execute␈α
the␈α
corresponding␈α
action.␈α
We␈αwill
␈↓also␈α
designate␈α
an␈α
initialization␈α
routine␈α
which␈α
will␈α
be␈α
executed␈α
at␈α
the␈α
beginning␈α
of␈α
the␈α
reduction.
␈↓Initialization: Let V[0:N] be a vector; and let i be a integer variable, initialized to 0.

␈↓␈↓ αλ    rule␈↓ β8␈↓ ¬h    action

␈↓␈↓ αλ<exp>␈↓ β8::= <exp> + <term>␈↓ ¬hV(i) ← `+'; i ← i+1;
␈↓␈↓ αλ␈↓ β8::= <term>␈↓ ¬h do nothing

␈↓␈↓ αλ<term>␈↓ β8::= <term>*<factor>␈↓ ¬hV(i) ← `*'; i ← i+1;
␈↓␈↓ αλ␈↓ β8::= <factor>␈↓ ¬h do nothing

␈↓␈↓ αλ<factor>␈↓ β8::= (<exp>)␈↓ ¬h do nothing
␈↓␈↓ αλ␈↓ β8::= <integer>␈↓ ¬h do nothing

␈↓␈↓ αλ<integer>␈↓ β8::= 0, 1, 2, ...␈↓ ¬hV(i) ← 0, 1 .... ; i ← i+1;


␈↓Again␈αperforming␈αthe␈αreduction␈αof␈αthe␈αexpression,␈α2+3*5,␈αbut␈αnow␈αexecuting␈αthe␈αaction␈αroutines␈αas
␈↓well␈α
we␈α
find␈α
the␈α
contents␈α
of␈α
V␈α
will␈α
contain␈α
the␈α
following:

␈↓              V: 0 1 2 3 4 5 ...
␈↓                  2
␈↓                  2 3
␈↓                  2 3 5
␈↓                  2 3 5 *
␈↓                  2 3 5 * +
␈↓␈↓↓82  Running on the machine␈↓ 45.3␈↓


␈↓That␈α
is,␈α
the␈α
postfix␈α
form␈α
of␈α
the␈α
arithmetic␈α
expression␈α
is␈α
formed␈α
in␈α
V.

␈↓So␈α
combining␈α
the␈α
two␈αalgorithms:␈α
infix␈α
to␈α
postfix␈α
translation,␈αand␈α
postfix␈α
evaluation␈α
we␈αcould␈α
write
␈↓an␈α
infix␈α
evaluator.␈α
However␈α
we␈α
can␈α
go␈α
one␈α
better.␈α
By␈α
a␈α
simple␈α
change␈α
to␈α
the␈α
action␈α
routines␈αwe␈α
can
␈↓perform␈α
infix␈α
evaluation␈α
as␈α
we␈α
reduce␈α
(or␈α
recognize)␈α
the␈α
expression.

␈↓Initialization:␈α
Let␈α
V[0:N]␈α
be␈α
a␈α
vector␈α
and␈α
let␈α
i␈α
be␈α
an␈α
integer-valued␈α
variable,␈α
initialized␈α
to␈α
0.

␈↓␈↓ αλ  rule␈↓ β8␈↓ ¬h  action

␈↓␈↓ αλ<exp>␈↓ β8::= <exp>+<term>␈↓ ¬hV(i-2) ← V(i-1)+V(i-2); i ← i-1;
␈↓␈↓ αλ␈↓ β8::= <term>␈↓ ¬h do nothing

␈↓␈↓ αλ<term>␈↓ β8::= <term>*<factor>␈↓ ¬hV(i-2) ← V(i-1)*V(i-2); i ← i-1;
␈↓␈↓ αλ␈↓ β8::= <factor>␈↓ ¬h do nothing

␈↓␈↓ αλ<factor>␈↓ β8::= (<exp>)␈↓ ¬h do nothing
␈↓␈↓ αλ␈↓ β8::= <integer>␈↓ ¬h do nothing

␈↓␈↓ αλ<integer>␈↓ β8::= 0, 1, ...␈↓ ¬hV(i) ← 0, 1, ... ; i ← i+1;


␈↓When␈α
the␈α
arithmetic␈α
expression␈α
has␈α
been␈α
recognized,␈α
V(0)␈α
will␈α
contain␈α
the␈α
value␈α
of␈α
that␈α
expression.

␈↓This␈α⊃technique␈α⊂of␈α⊃associating␈α⊃action␈α⊂routines␈α⊃(also␈α⊂called␈α⊃semantic␈α⊃routines)␈α⊂with␈α⊃the␈α⊃BNF␈α⊂(or
␈↓syntax)␈α∩equations␈α⊃is␈α∩extremely␈α⊃powerful.␈α∩ Such␈α⊃processes␈α∩are␈α⊃called␈α∩syntax-directed.␈α∩ When␈α⊃we
␈↓discuss␈α
compilation␈α
syntax-directed␈α
methods␈α
will␈α
be␈α
used.

␈↓␈↓ ε∨␈↓↓Project␈↓

␈↓Write␈αa␈αLISP␈αprogram␈αto␈αperform␈αinfix␈αto␈αpostfix␈αtranslation;␈αand␈αthen␈αmodify␈αit␈αto␈αperform␈αinfix
␈↓evaluation.

␈↓␈↓ ε∨␈↓↓Project␈↓

␈↓As␈αa␈αfurther␈αexample␈αof␈αsyntax-directed␈αprocesses␈αrecall␈αthe␈αset␈αof␈αexpressions␈αevaluated␈αby␈α
␈↓αtgmoaf␈↓:
␈↓the␈α⊃five␈α⊂primitives␈α⊃under␈α⊂composition␈α⊃of␈α⊂functions,␈α⊃all␈α⊂with␈α⊃constant␈α⊂arguments.␈α⊃ Write␈α⊂syntax
␈↓equations␈α
and␈α
action␈α
routines␈α
to␈α
effect␈α
the␈α
evaluation␈α
of␈α
such␈α
expressions.



␈↓␈↓ ∧e␈↓↓5.4  A project: Syntax-directed I/O␈↓


␈↓It␈α
is␈α
frequently␈αquite␈α
desirable␈α
and␈α
convenient␈αto␈α
enter␈α
input␈α
and␈αreceive␈α
output␈α
in␈αsomething␈α
other
␈↓␈↓↓5.4␈↓ πmA project: Syntax-directed I/O     83␈↓


␈↓than␈α
sexprs.␈α Recall␈α
our␈α
diagram␈αon␈α
page␈α25.␈α
Evaluation␈α
can␈αbe␈α
mechanized;␈αthat's␈α
␈↓αeval␈↓;␈α
what␈αwe
␈↓wish␈α⊃to␈α⊂do␈α⊃now␈α⊂is␈α⊃examine␈α⊂the␈α⊃possibility␈α⊂of␈α⊃mechanizing␈α⊂the␈α⊃encoding␈α⊂of␈α⊃the␈α⊂input␈α⊃and␈α⊂the
␈↓decoding␈α
of␈α
the␈α
output.

␈↓Consider␈α∀for␈α∃example,␈α∀the␈α∀problem␈α∃of␈α∀simplification␈α∀of␈α∃algebraic␈α∀expressions.␈α∃ Many␈α∀rather
␈↓sophisticated␈α∩simplifiers␈α∩have␈α∪been␈α∩written.␈α∩Assume␈α∩that␈α∪we␈α∩have␈α∩one␈α∩named␈α∪␈↓αsimplify␈↓␈α∩which
␈↓expects␈α
sexpr␈α
input␈α
and␈α
gives␈α
sexpr␈α
output.␈α
Thus␈α
for␈α
example
␈↓α␈↓ α¬(3+4)*x + x =␈↓βI ␈↓α=> (PLUS (TIMES (PLUS 3 4) X) X) =␈↓βsimplify␈↓α=> (TIMES 8 X) =␈↓βO ␈↓α=> 8*x.

␈↓We␈α
would␈α
like␈α
transformations␈α
I␈α
and␈α
O␈α
done␈α
automatically.␈α
Notice␈α
LISP␈α
is␈α
itself␈α
a␈α
candidate␈αfor
␈↓such␈α
a␈α
task.
␈↓α␈↓ αXcons[A;B] =␈↓βI ␈↓α=> (CONS (QUOTE A)(QUOTE B)) =␈↓βeval␈↓α=> (A . B) =␈↓βO ␈↓α=> (A . B)

␈↓Transformation␈α
O␈α
is␈α
the␈α
identity␈α
in␈α
this␈α
case.

␈↓In␈α⊂many␈α⊂cases␈α⊃the␈α⊂automatic␈α⊂generation␈α⊃of␈α⊂the␈α⊂I␈α⊃and␈α⊂O␈α⊂transformations␈α⊃can␈α⊂be␈α⊂done.␈α⊃Such␈α⊂a
␈↓program,␈α∞SDIO␈α∂(Syntax Directed Input-Output),␈α∞was␈α∞written␈α∂by␈α∞Lynn␈α∞Quam␈α∂of␈α∞the␈α∂Stanford␈α∞AI
␈↓Laboratories␈αin␈α1968.␈αIt␈αis␈αthe␈αforerunner␈αof␈αthe␈αmore␈αambitious␈αMLISP2␈αproject.␈α The␈αbasic␈αidea
␈↓is␈αsimple␈αenough.␈αWe␈α
will␈αassume␈αthat␈αthe␈α
input␈αand␈αoutput␈αsyntax␈α
is␈αspecified␈αin␈αBNF.␈αWith␈α
each
␈↓BNF␈α↔equation␈α↔we␈α_will␈α↔associate␈α↔semantics␈α↔describing␈α_the␈α↔sexpr␈α↔representation.␈α_The␈α↔input
␈↓transformation␈α∪(parser)␈α∪will␈α∪use␈α∪this␈α∪information␈α∪to␈α∪build␈α∪the␈α∪representation;␈α∪and␈α∪the␈α∩output
␈↓transformation␈α
(unparser)␈α
will␈α
map␈α
the␈α
internal␈α
representation␈α
back.

␈↓The␈α⊂importance␈α⊃of␈α⊂syntax␈α⊃directed␈α⊂I/O␈α⊃should␈α⊂not␈α⊃be␈α⊂minimized.␈α⊃One␈α⊂aspect␈α⊃of␈α⊂SDIO␈α⊃is␈α⊂the
␈↓ablility␈α∀to␈α∪define␈α∀new␈α∪data␈α∀types␈α∪in␈α∀LISP.␈α∀Assume␈α∪we␈α∀which␈α∪to␈α∀represent␈α∪a␈α∀structure␈α∀as␈α∪a
␈↓particularly␈αhorrible␈α
list␈αstructure.␈αWe␈α
can␈αgive␈αaugmented␈α
BNF␈αequations␈αspecifying␈α
the␈αexternal
␈↓representation␈α
and␈α∞the␈α
translation␈α
to␈α∞the␈α
underlying␈α
representation.␈α∞Clearly␈α
when␈α∞outputing␈α
these
␈↓structures␈α∂we␈α∂do␈α∂not␈α⊂want␈α∂to␈α∂see␈α∂the␈α∂internal␈α⊂representation.␈α∂This␈α∂can␈α∂be␈α⊂particularly␈α∂annoying
␈↓when␈αwe␈αare␈αdebugging;␈αwhen␈αwe␈αare␈αtrying␈αto␈αconcentrate␈αon␈αa␈αmisbehaving␈αalgorithm␈αwe␈αdo␈αnot
␈↓want␈αto␈αbe␈αdistracted␈αby␈αincomprehensible␈αoutput.␈α Thus␈αsyntax␈αdirected␈αoutput␈αor␈αunparsing␈αis␈αat
␈↓least␈α⊂as␈α⊂important␈α⊂as␈α⊂input.␈α⊃ Notice␈α⊂too␈α⊂that␈α⊂SDIO␈α⊂is␈α⊃a␈α⊂simple␈α⊂way␈α⊂to␈α⊂approach␈α⊃abstract␈α⊂data
␈↓structures.

␈↓Perhaps␈α∩the␈α∪easiest␈α∩introduction␈α∪to␈α∩SDIO␈α∪is␈α∩to␈α∪examine␈α∩an␈α∪example.␈α∩Consider␈α∪the␈α∩proposed
␈↓simplification␈α
task␈α∞above.␈α
The␈α
"natural"␈α∞input␈α
syntax␈α
can␈α∞be␈α
described␈α
in␈α∞BNF.␈α
 We␈α∞have␈α
given
␈↓closely␈αrelated␈αsyntax␈αequations␈αon␈αpage␈α80.␈α
We␈αwill␈αdisplay␈αa␈αfew␈αequations␈αaugmented␈α
by␈αSDIO
␈↓semantics.␈α
For␈α
example:

␈↓   <EXP>␈↓ βH::= <EXP> + <TERM>␈↓ λ(=>(PLUS EXP TERM)
␈↓␈↓ βH::= <TERM>␈↓ λ(=>*

␈↓   <TERM>␈↓ βH::= <NUMBER>␈↓ λ(=>*


␈↓To␈α∞the␈α∞input␈α∞parser␈α∞the␈α∞first␈α∞BNF␈α∞equation␈α∞means:␈α∞whenever␈α∞the␈α∞right␈α∞hand␈α∞side␈α∞is␈α
recognized,
␈↓␈↓↓84  Running on the machine␈↓ 15.4␈↓


␈↓reduce␈α∞that␈α∞occurrence␈α∞to␈α∞the␈α∞left␈α∞hand␈α∞side␈α∞and␈α∞associate␈α∞with␈α∞it␈α∞the␈α∞list␈α∞consisting␈α∞of␈α∂the␈α∞atom
␈↓PLUS,␈α∩the␈α⊃sexpr␈α∩associate␈α⊃with␈α∩the␈α⊃occurrence␈α∩of␈α⊃<EXP>,␈α∩and␈α⊃the␈α∩sexpr␈α⊃associated␈α∩with␈α⊃the
␈↓occurrrence␈α⊂of␈α⊃<TERM>.␈α⊂ The␈α⊃second␈α⊂equation␈α⊃means␈α⊂reduce␈α⊃<TERM>␈α⊂to␈α⊃<EXP>␈α⊂associating
␈↓whatever␈αsexpr␈αis␈αattached␈αto␈α<TERM>␈αwith␈αthat␈αoccurrence␈αof␈α<EXP>.␈αIn␈αthe␈αthird␈αequation␈αwe
␈↓assume␈α
that␈α<NUMBER>␈α
is␈α
a␈αsyntactic␈α
type␈α
recognized␈αby␈α
the␈α
scanner,␈αreturning␈α
that␈α
number␈αas
␈↓the␈α⊂semantic␈α∂value.␈α⊂ For␈α∂example␈α⊂if␈α∂such␈α⊂a␈α∂parser␈α⊂were␈α∂given␈α⊂2 +3+44␈α∂it␈α⊂should␈α∂return␈α⊂the␈α∂list
␈↓␈↓α(PLUS␈α
2␈α
(PLUS␈α
3␈α
44))␈↓.

␈↓The␈αunparser␈α
uses␈αthese␈αequations␈α
in␈αthe␈αopposite␈α
manner.␈αIt␈αwill␈α
see␈αa␈αsexpr␈α
and␈αwill␈α
attempt␈αto
␈↓match␈α
that␈α
to␈α
the␈α
description␈α
of␈α
the␈α
semantics,␈α
outputting␈α
an␈α
instance␈α
of␈α
the␈α
BNF␈α
if␈α
successful.

␈↓The␈αSDIO␈αprogram␈αwill␈αtake␈αsuch␈αan␈αaugmented␈αset␈αof␈αBNF␈αequations␈αand␈αgenerate␈αa␈αparser␈αand
␈↓an␈αunparser␈αfor␈αthe␈αlanguage.␈α This␈αproject␈α
involves␈αwriting␈αsuch␈αa␈αSDIO␈αprogram.␈αWe␈αdescribe␈α
a
␈↓basic␈α
SDIO␈α
program␈α
and␈α
suggest␈α
extensions␈α
and␈α
improvements.

␈↓The␈α
best␈α
way␈α
to␈α
describe␈α
the␈α
format␈α
of␈α
SDIO␈α
input␈α
is␈α
to␈α
give␈α
an␈α
SDIO␈α
description:

␈↓<RULES>␈↓ βH::= END␈↓ λ(=>NIL
␈↓␈↓ βH::=<RULE><RULES>␈↓ λ(=>(RULE . RULES)

␈↓<RULE>␈↓ βH::= <LFPT><RTLST>␈↓ λ(=>(LFPT RTLST)

␈↓<RTLST>␈↓ βH::= ::=<RTPT><SEXPR><RTLST>␈↓ λ(=>((RTPT SEXPR) . RTLST)
␈↓␈↓ βH::=␈↓ λ(=>NIL

␈↓<LFPT>␈↓ βH::= <<ID>>␈↓ λ(=>*

␈↓<RTPT>␈↓ βH::= "=>␈↓ λ(=>NIL
␈↓␈↓ βH::= <RPELEM><RTPT>␈↓ λ(=>(RPELEM . RTPT)

␈↓<RPELEM>␈↓ βH::= <<ID>>␈↓ λ(=>*
␈↓␈↓ βH::=<ID>␈↓ λ(=>(SPWD ID)
␈↓␈↓ βH::=""<CHAR>␈↓ λ(=>(QCH CHAR)
␈↓␈↓ βH::=<CHAR>␈↓ λ(=>(CH CHAR)

␈↓<SEXPR>␈↓ βH::= <ATOM>␈↓ λ(=>*
␈↓␈↓ βH::= (<SEXPRLIST>)␈↓ λ(=>*

␈↓<SEXPRLIST>␈↓ βH::= <ATOM>␈↓ λ(=>*
␈↓␈↓ βH::= <SEXPR> <SEXPRLIST>␈↓ λ(=>(SEXPR . SEXPRLIST)
␈↓␈↓ βH::=␈↓ λ(=>NIL

␈↓END

␈↓So␈α⊂the␈α⊂SDIO␈α⊂input␈α⊂is␈α⊂a␈α⊃sequence␈α⊂of␈α⊂augmented␈α⊂BNF␈α⊂equations␈α⊂terminated␈α⊂with␈α⊃END.␈α⊂ What
␈↓␈↓↓5.4␈↓ πmA project: Syntax-directed I/O     85␈↓


␈↓SDIO␈αprogram␈αsees␈αis␈αa␈αsexpr␈αrepresentation␈αof␈αthese␈αequations.␈αThe␈αsample␈αequations␈αfor␈α<EXP>
␈↓above␈α
would␈α
pass␈α
the␈α
following␈α
to␈α
the␈α
SDIO␈α
program.

␈↓␈↓ αX(␈↓ βλ(EXP(␈↓ ∧H((EXP (CH +) TERM) (PLUS EXP TERM))
␈↓␈↓ αX␈↓ βλ␈↓ ∧H((TERM) NIL)))
␈↓␈↓ αX␈↓ βλ(TERM(␈↓ ∧H((NUMBER) NIL)))))

␈↓What␈α
the␈α
SDIO␈α
program␈α
outputs␈α
are␈α
the␈α
parser␈α
and␈α
unparser.

␈↓The␈α∞elements␈α∞of␈α∞the␈α
BNF␈α∞equations␈α∞in␈α∞SDIO␈α
are␈α∞rather␈α∞standard:␈α∞syntactic␈α∞variables,␈α
identifiers
␈↓bracketed␈α∞by␈α∂"<"␈α∞and␈α∂">";␈α∞special␈α∂words,␈α∞which␈α∞are␈α∂identifiers;␈α∞and␈α∂special␈α∞characters,␈α∂which␈α∞are
␈↓preceeded␈α
by␈α
"␈α
if␈α
they␈α
conflict␈α
with␈α
the␈α
special␈α
characters␈α
of␈α
the␈α
BNF.

␈↓The␈α∞elements␈α∂of␈α∞the␈α∞semantics␈α∂are:␈α∞unbracketed␈α∞syntactic␈α∂variables␈α∞occurring␈α∞in␈α∂the␈α∞RHS␈α∂of␈α∞the
␈↓associated␈α∞BNF␈α∞equation;␈α∞other␈α∞identifiers,␈α∞taken␈α∂as␈α∞constants;␈α∞NIL,␈α∞the␈α∞LISP␈α∞atom;␈α∂notation␈α∞for
␈↓␈↓αcons␈↓-ing,␈α
␈↓α(   .   )␈↓;␈α
notation␈α
for␈α
making␈α
a␈α
list,␈α
␈↓α(e␈↓β1␈↓α   e␈↓βn␈↓α)␈↓;␈α
the␈α
character␈α
*,␈α
described␈α
above.

␈↓␈↓ ε∨␈↓↓Project␈↓

␈↓Write␈αsuch␈αa␈αSDIO␈αprogram.␈αYou␈αshould␈αconsult␈αlocal␈αdocumentation␈αand␈αthe␈αLISP␈αwizard␈αwhen
␈↓building␈α
the␈α
basic␈α
I/O␈α
routines␈α
like␈α
<NUMBER>,␈α
<CHAR>␈α
or␈α
<ID>.

␈↓␈↓ ¬h␈↓↓First Extension␈↓ 

␈↓You␈αshould␈αhave␈αnoticed␈αalready␈αthat␈αthe␈αbasic␈αSDIO␈αprogram␈αfails␈αto␈αdistinguish␈αtwo␈αoccurrences
␈↓of␈α
the␈α
same␈α
syntactic␈α
variable␈α
on␈α
the␈α
RHS␈α
of␈α
an␈α
equation.␈α
Thus␈α
an␈α
equation␈α
like:

␈↓<ZIP>␈↓ α8::= <ZAP> <FOO> <ZAP>  must be replaced by the pair:

␈↓<ZIP>␈↓ α8::= <ZAP> <FOO> <ZAP1>
␈↓<ZAP1>␈↓ α8::=<ZAP>

␈↓This␈α
trick␈α
is␈α
called␈α
stratification.␈α
It␈α
is␈α
a␈α
syntactic␈α
trick,␈α
adding␈α
nothing␈α
to␈α
the␈α
semantics.

␈↓Add␈αnotation␈αto␈αthe␈αsemantics␈αof␈αyour␈αSDIO␈αprogram␈αto␈αhandle␈αRHS␈αwith␈αmultiple␈αoccurences␈αof
␈↓syntactic␈α
variables.␈α
Modify␈α
your␈α
parser␈α
generators␈α
accordingly.

␈↓␈↓ ¬Y␈↓↓Second Extension␈↓ 

␈↓Besides␈α∞building␈α∂a␈α∞sexpr␈α∞representation␈α∂of␈α∞the␈α∞input,␈α∂it␈α∞is␈α∞frequently␈α∂desirable␈α∞to␈α∂generate␈α∞other
␈↓information␈αduring␈αthe␈α
input␈αparse.␈αLists␈α
of␈αoccurences␈αof␈αoperators␈α
or␈αother␈αtables␈α
are␈αcommonly
␈↓needed.␈α∂The␈α∞additional␈α∂information␈α∞could␈α∂be␈α∞discovered␈α∂by␈α∞examination␈α∂of␈α∞the␈α∂completed␈α∞parse
␈↓tree␈αbut␈αthat␈αrequires␈αreexamination␈αof␈αthe␈αtree.␈αIt␈α
is␈αmuch␈αmore␈αefficient␈αto␈αto␈αas␈αmuch␈αas␈α
possible
␈↓on␈α
a␈α
single␈α
pass.
␈↓␈↓↓86  Running on the machine␈↓ 15.4␈↓


␈↓Introduce␈α
notation␈α
which␈α
will␈α
allow␈α
execution␈α
of␈α
arbitrary␈α
LISP␈α
code␈α
as␈α
the␈α
parse␈α
is␈α∞in␈α
progress.
␈↓That␈α
code␈αshould␈α
be␈α
able␈αto␈α
manipulate␈α
any␈αof␈α
the␈α
semantic␈αproperties␈α
associated␈α
with␈αthe␈α
syntactic
␈↓variables␈α
appearing␈α
in␈α
the␈α
RHS␈α
of␈α
the␈α
associated␈α
syntax␈α
equation.

␈↓␈↓ ¬d␈↓↓Third extension␈↓ 

␈↓While␈α⊃it␈α⊂is␈α⊃obviously␈α⊂advantageous␈α⊃to␈α⊃produce␈α⊂output␈α⊃in␈α⊂the␈α⊃language␈α⊂described␈α⊃by␈α⊃the␈α⊂BNF
␈↓equations␈α⊂rather␈α⊂than␈α⊂the␈α∂sexpr␈α⊂form,␈α⊂formatting␈α⊂of␈α⊂the␈α∂output␈α⊂can␈α⊂be␈α⊂equally␈α⊂beneficial.␈α∂ We
␈↓should␈αlike␈αto␈αbe␈αable␈αto␈αspecify␈αformatting␈αinformation␈αin␈αSDIO␈αsuch␈αthat␈αspacing␈αand␈α
line-length
␈↓are␈α
controlled.

␈↓One␈α⊂proposal␈α∂is␈α⊂to␈α∂embed␈α⊂spacing␈α∂and␈α⊂line-feed␈α∂control␈α⊂characters␈α∂in␈α⊂the␈α∂BNF␈α⊂equations.␈α∂The
␈↓spacing␈α
character␈α
is␈α
"→"␈α
and␈α
the␈α
line-feed␈α
is␈α
"↓".␈α
 The␈α
"↑"␈α
sets␈α
the␈α
indentation␈α
point␈α
for␈α
the␈α
string␈α
on
␈↓its␈α∂right;␈α∂and␈α∂the␈α∂"→"␈α∂followed␈α∂by␈α∂a␈α∂digit␈α∂says␈α∂space␈α∂over␈α∂than␈α∂number␈α∂of␈α∂spaces␈α∂from␈α⊂the␈α∂last
␈↓indentation␈αpoint␈αif␈αthe␈αremaining␈α
space␈αon␈αthe␈αline␈αis␈αnot␈α
sufficient␈αto␈αcontain␈αall␈αtext␈αspecified␈α
by
␈↓the␈α
remaining␈α
RHS␈α
of␈α
the␈α
equation.␈α
 "→"0,␈α
meaning␈α
go␈α
to␈α
the␈α
indentation␈α
point␈α
can␈α
be␈α
written␈α
"→".

␈↓For␈α
example␈α
consider␈α
the␈α
following:

␈↓␈↓ αX<EXPR>␈↓ ∧8::= <ID>( ↓ <EXPR_LIST>)
␈↓␈↓ αX␈↓ ∧8::= <ID>

␈↓␈↓ αX<EXPR_LIST>␈↓ ∧8::= ↓<EXPR> , →<EXPR_LIST>
␈↓␈↓ αX␈↓ ∧8::= ↓<EXPR>

␈↓These␈α
equations,␈α
when␈α
used␈α
to␈α
drive␈α
an␈α
unparser␈α
could␈α
result␈α
in:

␈↓mumf(␈↓ α(a,
␈↓␈↓ α(foobaz(garp(b),
␈↓␈↓ α(bletch(a,b,c),
␈↓␈↓ α(d)

␈↓Extend␈α
SDIO␈α
to␈α
handle␈α
formatting␈α
of␈α
output.
␈↓␈↓↓6.␈↓ 	Psymbol tables     87␈↓


␈↓␈↓ ¬␈␈↓↓SECTION 6

␈↓↓␈↓ ββTOWARD BETTER SYMBOL TABLES AND IMPLEMENTATION␈↓




␈↓*******diatribe␈α
about␈α
importance␈α
of␈α
LISP␈α
for␈α
implementors****

␈↓There␈αare␈α
some␈αrather␈α
gross␈αdefects␈αin␈α
our␈αsymbol␈α
table␈αmechanism.␈α
 First,␈α(disregarding␈α␈↓αdefine␈↓)␈α
we
␈↓have␈αhad␈αto␈αresort␈α
to␈αsubterfuge␈αto␈αput␈α
the␈αdefinitions␈αof␈αour␈α
functions␈αin␈αour␈αsymbol␈αtable.␈α
 Using
␈↓␈↓αlabel␈↓␈α
(Section␈α
4.11),␈α
or␈α
calling␈α␈↓αeval␈↓␈α
with␈α
an␈α
initial␈α
symbol␈αtable,␈α
only␈α
defines␈α
the␈α
function␈α
for␈α␈↓↓that␈↓
␈↓particular␈α∪call␈α∪on␈α∩␈↓αeval␈↓.␈α∪When␈α∪we␈α∩finish␈α∪the␈α∪computation,␈α∩the␈α∪definition␈α∪disappears.␈α∪ From␈α∩a
␈↓practical␈α⊂point␈α⊂of␈α⊂view␈α⊂definitions␈α⊂should␈α⊂have␈α⊂some␈α⊂permanence.␈α⊂ An␈α⊂implemented␈α⊃version␈α⊂of
␈↓LISP␈αshould␈αknow␈αa␈α
lot␈αmore␈αfunctions␈αthan␈α
the␈αfive␈αprimitives.␈α It␈α
should␈αhave␈αa␈αreasonable␈α
class
␈↓of␈α
arithmetic␈α∞functions,␈α
and␈α
some␈α∞commonly␈α
used␈α∞Sexpr␈α
functions␈α
(like␈α∞␈↓αlength,␈α
assoc,␈α∞subst,␈α
equal,␈↓
␈↓etc.)␈αall␈αpredefined.␈α If␈αwe␈αdo␈αthings␈αright␈αwe␈αshould␈αbe␈αable␈αto␈αuse␈αthe␈αmechanism␈αfor␈αpredefining
␈↓functions␈α
as␈α
the␈α
tool␈α
for␈α
adding␈α
our␈α
own␈α
definitions.

␈↓Second,␈α
the␈α
table␈α
look-up␈α
mechanism␈α
of␈α∞␈↓αassoc␈↓,␈α
though␈α
theoretically␈α
sound,␈α
leaves␈α
something␈α∞to␈α
be
␈↓desired␈α⊃as␈α⊂far␈α⊃as␈α⊂efficiency␈α⊃is␈α⊂concerned.␈α⊃Since␈α⊂we␈α⊃do␈α⊂wish␈α⊃to␈α⊂implement␈α⊃LISP,␈α⊂we␈α⊃should␈α⊂be
␈↓concerned␈α∩with␈α∪efficient␈α∩operations.␈α∪We␈α∩will␈α∪see␈α∩that␈α∪an␈α∩efficient␈α∪and␈α∩powerful␈α∪symbol␈α∩table
␈↓structure␈α
can␈α
be␈α
described␈α
quite␈α
easily␈α
in␈α
LISP.

␈↓Third,␈α∀we␈α∃have␈α∀already␈α∀seen␈α∃that␈α∀special␈α∀forms␈α∃␈↓αQUOTE␈↓␈α∀and␈α∀␈↓αCOND␈↓␈α∃are␈α∀not␈α∃evaluated␈α∀in
␈↓call-by-value␈α∞style.␈α∞Currently␈α∞the␈α∞recognizers␈α∞for␈α∞special␈α∞forms␈α∞are␈α∞encoded␈α∞in␈α∞␈↓αeval␈↓,␈α∞and␈α∞when␈α
an
␈↓instance␈α
of␈α
such␈α
a␈α
form␈α
is␈α
seen␈α
the␈αargument␈α
is␈α
passed␈α
␈↓↓unevaluated␈↓␈α
to␈α
a␈α
function␈α
to␈α
perform␈αthe
␈↓required␈αoperations.␈α It␈αwould␈αbe␈αnice␈αto␈αextend␈αthis␈αflexibility␈αto␈αthe␈αuser.␈αWe␈αwould␈αlike␈αto␈αhave
␈↓notation␈αfor␈αadding␈αλ-definitions␈αof␈αspecial␈αforms␈αto␈αour␈αsymbol␈αtable.␈α␈↓αeval␈↓␈αwould␈αthen␈αneed␈αa␈αway
␈↓of␈α
distinguishing␈α
a␈α
λ-expression␈α
defining␈α
a␈α
function␈α
from␈α
a␈α
λ-expression␈α
defining␈α
a␈α
special␈α
form.
␈↓Also␈αthere␈αare␈αother␈αcalling␈αsequences␈αwhich␈αare␈αinteresting␈αand␈αuseful␈αand␈αwould␈αbe␈αnice␈αto␈αhave.
␈↓The␈αcurrent␈α
version␈αof␈α␈↓αeval␈↓␈α
only␈αhandles␈αfunction␈α
definitions.␈αThus␈αthe␈α
current␈αsymbol␈α
need␈αonly
␈↓store␈α
the␈α
λ-definition␈α
and␈α
does␈α
not␈α
need␈α
explicit␈α
information␈α
saying␈α
it␈α
is␈α
a␈α
␈↓↓function␈↓␈α
definition.

␈↓Fourth,␈αconceptually␈αwe␈αcould␈αuse␈αa␈αmore␈αpowerful␈αmechanism.␈α Consider␈α␈↓αcar[car]␈↓␈αas␈αan␈αexpression
␈↓to␈αbe␈αevaluated.␈αPerhaps␈αit␈αis␈αbest␈αjust␈αto␈αsay␈αthe␈αexpression␈αis␈αill-formed,␈αbut␈αif␈αyou␈αknew␈αthat␈α␈↓αcar␈↓
␈↓also␈α
was␈α
currently␈α
being␈α
used␈α
as␈α
a␈α
simple␈α
variable␈α
besides␈α
being␈α
a␈α
function,␈α
then␈α
your␈αintuition␈α
says
␈↓the␈α∂first␈α∂occurrence␈α∂of␈α∂␈↓αcar␈↓␈α∞is␈α∂a␈α∂function␈α∂name␈α∂and␈α∞the␈α∂second␈α∂occurrence␈α∂is␈α∂the␈α∂simple␈α∞variable
␈↓name;␈αand␈αif␈α
the␈αcurrent␈αvalue␈α
of␈α␈↓αcar␈↓␈αwas␈α
say,␈α␈↓α(A␈αB)␈↓␈α
then␈α␈↓αcar[car]␈↓␈αshould␈α
evaluate␈αto␈α␈↓αA␈↓.␈α
 That␈αis,
␈↓context␈α⊂tells␈α⊂us␈α∂which␈α⊂occurrence␈α⊂of␈α∂␈↓αcar␈↓␈α⊂is␈α⊂a␈α⊂function␈α∂and␈α⊂which␈α⊂is␈α∂a␈α⊂simple␈α⊂variable␈α⊂␈↓π 8␈↓.␈α∂ The
␈↓current␈α∩␈↓αeval␈↓␈α∩␈↓↓will␈↓␈α⊃operate␈α∩correctly␈α∩on␈α∩this␈α⊃example␈α∩since␈α∩a␈α∩recognizer␈α⊃for␈α∩the␈α∩function␈α∩␈↓αcar␈↓␈α⊃is


________________
␈↓␈↓π 8␈↓␈α∂just␈α∂as␈α∂an␈α∂evaluator␈α∂for␈α∂␈↓αprog␈↓␈α∂can␈α∂tell␈α∞a␈α∂reference␈α∂to␈α∂the␈α∂label␈α∂␈↓αx␈↓␈α∂from␈α∂the␈α∂␈↓αprog␈↓-variable␈α∂␈↓αx␈↓␈α∞in
␈↓␈↓α ... prog[[x;y] .. x f[..] ... g[x .. ] ... go[x].␈↓␈α
See␈α
page␈α
67.
␈↓␈↓↓88  symbol tables␈↓ C6.␈↓


␈↓explicitly␈α∞encoded␈α∞in␈α
␈↓αapply␈↓.␈α∞ ␈↓π 9␈↓.␈α∞ However,␈α∞in␈α
general␈α∞our␈α∞current␈α
symbol␈α∞table␈α∞mechanism␈α∞is␈α
not
␈↓sufficient␈α∞for␈α∞this␈α∂interpretation.␈α∞Forced␈α∞to␈α∂use␈α∞␈↓αassoc␈↓,␈α∞we␈α∂would␈α∞only␈α∞find␈α∂the␈α∞␈↓↓first␈↓␈α∞binding␈α∂of␈α∞a
␈↓variable.␈αIt␈α
will␈αeither␈αbe␈α
a␈αλ-expression␈α(function)␈α
or␈αa␈αsimple␈α
value.␈αContext␈αwill␈α
determine␈αhow
␈↓␈↓αeval␈↓␈αwill␈αinterpret␈αwhat␈αis␈αfound.␈α Clearly␈αwhat␈αis␈αneeded␈αis␈αthe␈αability␈αto␈αassociate␈αmore␈α
than␈αone
␈↓property␈αwith␈αan␈α
atom.␈αIn␈αthe␈α
above␈αexample␈α␈↓αcar␈↓␈αhas␈α
(at␈αleast)␈αtwo␈α
␈↓↓properties␈↓␈αor␈α␈↓↓attributes␈↓.␈α It␈α
has
␈↓a␈αfunction␈αvalue␈α
and␈αit␈αhas␈αa␈α
simple␈αvalue.␈αSince␈α
␈↓αcar␈↓␈αis␈αa␈αprimitive␈α
function,␈αits␈αfunction␈α
value␈αis
␈↓the␈α⊃location␈α⊂of␈α⊃the␈α⊂machine␈α⊃code␈α⊃to␈α⊂carry␈α⊃out␈α⊂the␈α⊃␈↓αcar␈↓␈α⊃function;␈α⊂the␈α⊃simple␈α⊂value␈α⊃is␈α⊃the␈α⊂sexpr
␈↓currently␈α
bound␈α
to␈α
the␈α
variable,␈α
␈↓αcar␈↓.

␈↓These␈αlast␈αthree␈αdesires,␈αfor␈αpermanence,␈αgenerality,␈αand␈αfor␈αmultiple␈αproperties,␈αcan␈αbe␈αhandled␈αby
␈↓the␈αsame␈αmechanism.␈αClearly␈αwe␈αcould␈α
continue␈αusing␈αan␈α␈↓αassoc␈↓-like␈αtable,␈αthough␈αmore␈α
complicated,
␈↓storing␈α
information␈α
about␈α
what␈αkind␈α
of␈α
value␈α
each␈α
dotted␈αpair␈α
is.␈α
However␈α
our␈α
desire␈αfor␈α
efficiency
␈↓would␈α
certainly␈α∞not␈α
be␈α
satisfied.␈α∞Rather,␈α
we␈α∞will␈α
design␈α
a␈α∞new␈α
symbol␈α
table␈α∞which␈α
can␈α∞store␈α
with
␈↓each␈α
atom␈α
sequences␈α
of␈α
values␈αand␈α
their␈α
types.␈α
This␈α
gives␈αus␈α
multiple␈α
properties.␈α
 We␈α
will␈αmake␈α
the
␈↓table␈α␈↓↓global␈↓␈αto␈α␈↓αeval␈↓␈αand␈α␈↓αapply␈↓;␈αthese␈αfunctions␈αwill␈αnow␈αimplicitly␈αrefer␈αto␈αthat␈αtable␈αand␈αthus␈αneed
␈↓not␈α⊃have␈α⊃an␈α⊃explicit␈α∩symbol-table␈α⊃argument.␈α⊃This␈α⊃gives␈α∩us␈α⊃permanence.␈α⊃We␈α⊃will␈α∩designate␈α⊃an
␈↓indicator␈αfor␈αfunction␈αdefinition␈αand␈αan␈αindicator␈αfor␈αspecial␈αform␈αdefinition␈αand␈αexpand␈α␈↓αeval␈↓␈αand
␈↓␈↓αapply␈↓␈α
to␈α
recognize␈α
these␈α
indicators.␈α
 This␈α
gives␈α
us␈α
generality.

␈↓Most␈αimplementations␈α
of␈αLISP␈α
allow␈αthis␈α
association␈αof␈α
arbitrary␈αsequences␈α
of␈α␈↓¬attribute-value␈↓␈α
pairs
␈↓with␈αan␈αatom.␈α It␈αis␈αa␈αvery␈αgood␈αidea.␈α How␈αcan␈αwe␈αassociate␈αan␈αarbitrary␈αcollection␈αof␈αobjects␈αwith
␈↓something?␈α∞ With␈α∞each␈α∞atom␈α∞we␈α∞will␈α∞associate␈α∞a␈α∞list␈α∞called␈α∞the␈α∞␈↓¬property-list␈↓␈α∞or␈α∞p␈↓¬-list.␈↓␈α∞But␈α∞atoms
␈↓can't␈α
be␈αrepresented␈α
as␈αjust␈α
any␈α
other␈αlist.␈α
 Among␈αother␈α
things,␈α
we␈αmust␈α
be␈αable␈α
to␈α
recognize␈αthe
␈↓occurrence␈α
of␈α
atoms␈α
so␈α
that␈α
we␈α
can␈α
implement␈α
the␈α
predicate,␈α
␈↓αatom␈↓.␈α
 So␈α
atoms␈α
are␈α
special␈α
lists:␈αthe␈α
␈↓αcar␈↓
␈↓(or␈α∩first␈α∩element)␈α⊃of␈α∩the␈α∩representation␈α⊃of␈α∩each␈α∩atom␈α⊃is␈α∩an␈α∩indicator␈α⊃used␈α∩exclusively␈α∩for␈α⊃the
␈↓beginning␈α∩of␈α∩atoms.␈α⊃We␈α∩will␈α∩use␈α⊃␈↓εα␈↓␈α∩to␈α∩designate␈α∩the␈α⊃beginning␈α∩of␈α∩an␈α⊃atom.␈α∩ The␈α∩␈↓αcdr␈↓␈α∩of␈α⊃the
␈↓representation␈α∞of␈α∞the␈α
atom␈α∞is␈α∞the␈α
property␈α∞list.␈α∞The␈α
elements␈α∞of␈α∞the␈α
p-list␈α∞are␈α∞associated␈α∞in␈α
pairs.
␈↓The␈α∂first␈α∂element␈α∂is␈α∂the␈α∞attribute␈α∂(or␈α∂property␈α∂or␈α∂indicator);␈α∞the␈α∂next␈α∂element␈α∂is␈α∂the␈α∂value.␈α∞ For
␈↓example,␈α∂here␈α∂is␈α∞part␈α∂of␈α∂the␈α∞atom-structure␈α∂for␈α∂␈↓αcar␈↓␈α∂assuming␈α∞␈↓αcar␈↓␈α∂is␈α∂also␈α∞being␈α∂used␈α∂as␈α∂a␈α∞simple
␈↓variable␈α
and␈α
has␈α
current␈α
value,␈α
␈↓α(A␈α
B)␈↓:







␈↓␈↓ ¬*␈↓↓Atom-structure for ␈↓αCAR␈↓. 

␈↓The␈α
indicator,␈α
␈↓αSUBR␈↓,␈αtells␈α
us␈α
that␈α
␈↓αcar␈↓␈αis␈α
a␈α
machine␈α
language␈αfunction.␈α
 The␈α
indicator,␈α␈↓αVALUE␈↓,␈α
tells
␈↓us␈αthat␈α
␈↓αcar␈↓␈αis␈αalso␈α
being␈αused␈α
as␈αa␈αsimple␈α
variable.␈α The␈α
reason␈αfor␈αnot␈α
pointing␈αdirectly␈α
at␈α␈↓α(A␈αB)␈↓␈α
is
␈↓described␈α∩in␈α∩Section␈α∩7.11.

________________
␈↓␈↓π 9␈↓␈αFor␈αthe␈α
same␈αreason,␈αthe␈α
LISP␈αobscenity␈α␈↓αλ[[lambda]␈α
...␈α]␈↓␈αwill␈α
work.␈αNotice␈αits␈αsexpr␈α
representation
␈↓is␈α
␈↓α(LAMBDA (LAMBDA) ␈α
... )␈↓
␈↓␈↓↓6.␈↓ 	Psymbol tables     89␈↓


␈↓It␈α∞should␈α
now␈α∞be␈α
clear␈α∞how␈α∞to␈α
program␈α∞the␈α
primitive␈α∞predicate,␈α∞␈↓αatom␈↓:␈α
"is␈α∞␈↓αcar␈↓␈α
of␈α∞the␈α∞argument␈α
to
␈↓␈↓αatom␈↓␈α
the␈α
special␈α
atom-indicator?"␈α
If␈α
it␈α
is␈α
then␈α
␈↓αatom␈↓␈α
gives␈α
value␈α
␈↓αT␈↓,␈α
otherwise␈α
␈↓αatom␈↓␈α
gives␈α
value␈α
␈↓αNIL␈↓.

␈↓How␈α⊂about␈α⊂the␈α⊂representation␈α⊂of␈α⊂non-primitive␈α⊂functions?␈α⊂ Non-primitive␈α⊂functions␈α⊂are␈α∂defined
␈↓using␈αλ-expressions.␈α What␈αwe␈αdo␈αis␈αdefine␈αa␈αnew␈αindicator,␈α␈↓αEXPR␈↓,␈αwhich␈αtells␈αthe␈αevaluator␈αthat
␈↓the␈α⊂function␈α⊂is␈α∂a␈α⊂λ-expression.␈α⊂ For␈α∂example,␈α⊂here␈α⊂is␈α∂part␈α⊂of␈α⊂the␈α∂atom-structure␈α⊂for␈α⊂␈↓αFACT␈↓,␈α∂the
␈↓Sexpr␈α
form␈α
of␈α
the␈α
factorial␈α
function.
















␈↓␈↓ ¬ ␈↓↓Atom-structure for ␈↓αFACT␈↓. 

␈↓This␈α∞picture␈α
should␈α∞tell␈α
you␈α∞at␈α
least␈α∞two␈α
things:␈α∞one,␈α
that␈α∞programs␈α
are␈α∞(almost)␈α
stored␈α∞as␈α
binary
␈↓trees,␈α∪and␈α∪two,␈α∪it␈α∪should␈α∪tell␈α∪you␈α∪what␈α∩the␈α∪function,␈α∪␈↓αdefine␈↓,␈α∪does.␈α∪␈↓αdefine␈↓␈α∪(page␈α∪74)␈α∪puts␈α∩the
␈↓λ-definition␈α
under␈α
the␈α
indicator␈α
␈↓αEXPR␈↓␈α
on␈α
each␈α
of␈α
the␈α
named␈α
atoms␈α
(functions).

␈↓The␈α
world␈α
becomes␈α∞much␈α
simpler␈α
if␈α∞we␈α
store␈α
each␈α∞atom␈α
uniquely.␈α
 This␈α∞is␈α
the␈α
reason␈α∞for␈α
saying
␈↓"almost"␈α
above.␈α∞ That␈α
is,␈α∞every␈α
reference␈α∞to␈α
the␈α∞atom␈α
␈↓αA␈↓␈α∞is␈α
actually␈α∞a␈α
pointer␈α∞to␈α
the␈α∞same␈α
"binary
␈↓tree",␈α
the␈α
binary␈αtree␈α
whose␈α
␈↓αcar␈↓-part␈αis␈α
the␈α
special␈αatom␈α
indicator,␈α
and␈αwhose␈α
␈↓αcdr␈↓-part␈α
is␈α
the␈αp-list
␈↓for␈α
the␈α
atom␈α
␈↓αA␈↓.␈α
Thus␈α
␈↓α(A␈α
.␈α
A)␈↓,␈α
which

␈↓we␈α
have␈α
been␈α
representing␈α
as:

␈↓␈↓ ¬_␈↓ε[~~~][~~~]
␈↓ε␈↓ ¬_␈↓α  A        A

␈↓is more realistically represented as:

␈↓␈↓ ¬_␈↓ε[~~~][~~~]


␈↓ε␈↓ ¬_␈↓                to atom-structure for ␈↓αA␈↓.
␈↓␈↓↓90  symbol tables␈↓ C6.␈↓


␈↓So␈α
the␈αinternal␈α
structures␈α
of␈αthis␈α
implementation␈α
of␈αLISP␈α
are␈α
not␈αbinary␈α
trees;␈α
there␈αare␈α
intersecting
␈↓branches.␈α
Structures␈α
of␈α
this␈α
sort␈α
we␈α
will␈αcall␈α
␈↓↓list␈α
structure␈↓.␈α
LISP␈α
thus␈α
deals␈α
generally␈α
with␈αbinary
␈↓list␈α∂structure.␈α∂ Trees␈α∂are␈α∂a␈α∂subset␈α∞of␈α∂list␈α∂structures.␈α∂ We␈α∂will␈α∂see␈α∞in␈α∂a␈α∂moment␈α∂that␈α∂many␈α∂of␈α∞the
␈↓computations␈α
which␈α
we␈α
have␈α
been␈α
performing␈α
have␈α
also␈α
been␈α
generating␈α
list␈α
structure.

␈↓Question:␈αAssume␈αwe␈αhave␈αthe␈αabove␈αdotted␈αpair␈αas␈αa␈αvalue␈αof␈αa␈αvariable␈α␈↓αx␈↓␈αand␈αwe␈αwish␈αto␈αprint
␈↓the␈α
value␈α
of␈α
␈↓αx␈↓.␈α
 Clearly␈α
we␈α
would␈α
hope␈α
to␈α
see␈α
"␈↓α(A␈α
.␈α
A)␈↓"␈α
appear␈α
on␈α
the␈α
output␈α
device.␈α
 We␈α
would
␈↓expect␈αthat␈αthe␈αprint␈αroutine,␈αnamed␈α␈↓αprint␈↓,␈αwould␈αbe␈αgiven␈αa␈αpointer␈αto␈αthe␈αdotted␈αpair.␈α
␈↓αprint␈↓␈αcan
␈↓recognize␈α
that␈α
it␈α
␈↓↓is␈↓␈α
a␈α
dotted␈α
pair␈α
since␈α
␈↓αcar␈↓␈α
of␈α
it␈α
is␈α
not␈α
␈↓εα␈↓.␈α
 But␈α
how␈α
can␈α
␈↓αprint␈↓␈α
distinguish␈α
␈↓α(A␈α∞.␈α
A)␈↓
␈↓from␈α
␈↓α(B␈α
.␈αB)␈↓␈α
for␈α
example.␈αThe␈α
pointer␈α
in␈αthe␈α
preceeding␈α
diagram␈α
will␈αpoint␈α
to␈α
␈↓αA␈↓␈αin␈α
one␈α
case␈αand␈α
to
␈↓␈↓αB␈↓␈αin␈α
the␈αother␈αbut␈α
nothing␈αin␈αour␈α
representation␈αtells␈α
us␈α␈↓↓what␈↓␈αto␈α
print.␈α The␈αsimplest␈α
thing␈αto␈αdo␈α
is
␈↓to␈α∞store␈α∂in␈α∞each␈α∂atom-structure␈α∞some␈α∂information␈α∞telling␈α∞what␈α∂the␈α∞name␈α∂of␈α∞the␈α∂atom␈α∞is.␈α∂ This␈α∞is
␈↓done␈α∞with␈α∞another␈α∞indicator␈α∞called␈α∞␈↓αPNAME␈↓,␈α∞standing␈α∞for␈α∞␈↓↓print-name␈↓.␈α∞Thus␈α∞the␈α∞atom␈α∞␈↓αBAZ␈↓␈α∞will
␈↓have␈α
at␈α
least␈α
the␈α
following:







␈↓␈↓ ¬+␈↓↓Atom-structure for ␈↓αBAZ␈↓. 

␈↓The␈αindicator␈αis␈αcalled␈α␈↓αPNAME␈↓␈αbecause␈αthe␈αprint-name␈α
(or␈αp-name)␈αof␈αthe␈αatom␈αis␈αwhat␈αthe␈α
LISP
␈↓output␈α∂routine␈α∂will␈α∞print␈α∂as␈α∂the␈α∞name␈α∂of␈α∂the␈α∞atom.␈α∂␈↓↓BAZ##␈↓␈α∂means␈α∞a␈α∂memory␈α∂location␈α∞containing
␈↓some␈α
encoding␈α
of␈α
the␈α
letters␈α
␈↓↓B␈↓,␈α
␈↓↓A␈↓,␈αand␈α
␈↓↓Z␈↓.␈α
 The␈α
symbol,␈α
␈↓↓#␈↓,␈α
represents␈α
some␈α
non-printing␈αcharacter;
␈↓thus␈αwe␈αare␈αassuming␈αa␈αlocation␈αcan␈αcontain␈αfive␈αcharacters.␈α We␈αrepresent␈αthe␈αprint-name␈αas␈αa␈αlist
␈↓so␈α
that␈α
we␈α
may␈α
allow␈α
atoms␈α
with␈αp-names␈α
of␈α
unbounded␈α
length.␈α
Thus␈α
the␈α
p-name␈α
for␈α␈↓αBLETCH␈↓,
␈↓would␈α
be:







␈↓␈↓ ∧{␈↓↓P-name structure for ␈↓αBLETCH␈↓. 

␈↓How␈αdo␈αwe␈αget␈αatoms␈αstored␈αuniquely?␈α ␈↓αread␈↓␈αdoes␈αit.␈αOn␈αreading␈αan␈αatom␈αname␈α(which␈αis␈αthe␈αway
␈↓almost␈α∂all␈α∞atoms␈α∂come␈α∞into␈α∂existence),␈α∞the␈α∂␈↓αread␈↓␈α∞program␈α∂checks␈α∞the␈α∂current␈α∞symbol␈α∂table.␈α∂ If␈α∞the
␈↓atom␈αdoes␈αnot␈αappear␈αwe␈αadd␈αa␈αnew␈αentry␈αconsisting␈αof␈αthe␈αp-list␈αwith␈αa␈αsingle␈αattribute-value␈αpair
␈↓---- the ␈↓αPNAME␈↓, p-name pair ---.␈α∃If␈α∃the␈α∃atom␈α∃␈↓↓does␈↓␈α∃appear,␈α∃we␈α∃bring␈α∃back␈α∃a␈α∃pointer␈α∃to␈α∃the
␈↓appropriate␈α
entry.

␈↓Before␈α∞talking␈α∂more␈α∞about␈α∞␈↓αread,␈α∂print␈↓␈α∞and␈α∞the␈α∂other␈α∞input-output␈α∞functions␈α∂in␈α∞LISP,␈α∂we␈α∞should
␈↓␈↓↓6.␈↓ 	Psymbol tables     91␈↓


␈↓discuss␈α⊃the␈α⊃storage␈α⊃of␈α∩list␈α⊃structure.␈α⊃The␈α⊃implementation␈α∩described␈α⊃closely␈α⊃follows␈α⊃that␈α∩for␈α⊃the
␈↓PDP-10,␈α
though␈α
most␈α
of␈α
the␈α
description␈α
is␈α
machine␈α
independent.

␈↓First,␈α
though␈αSexprs␈α
and␈αnow␈α
atoms␈αare␈α
represented␈αas␈α
binary␈αlist␈α
structure,␈αthere␈α
will␈αbe␈α
quantities
␈↓which␈αwe␈α
wish␈αto␈α
represent␈αin␈α
memory␈αwhich␈αare␈α
not␈αstructures.␈α
 For␈αexample,␈α
numeric␈αconstants,
␈↓and␈α⊂the␈α⊂actual␈α⊂encoding␈α⊂of␈α⊂print-names␈α⊂for␈α⊂atoms␈α⊂are␈α⊂not␈α⊂structures.␈α⊂ These␈α⊂quantities␈α⊃will␈α⊂be
␈↓stored␈αin␈αan␈αarea␈αcalled␈α␈↓↓full␈αword␈αspace␈α(FWS)␈↓.␈αMost␈αof␈αthe␈αbusiness␈αof␈αLISP␈αis␈αmassaging␈αof␈αlist
␈↓structure␈αand␈αbinary␈α
trees,␈αhowever;␈αsuch␈α
nodes,␈αwhich␈αhave␈α
a␈α␈↓αcar␈↓-part␈αand␈α
a␈α␈↓αcdr␈↓-part␈αare␈αstored␈α
in
␈↓a␈α
separate␈α
area␈αcalled␈α
␈↓↓free-space␈α
(FS)␈↓.␈α
 The␈αname,␈α
free␈α
space,␈α
will␈αbecome␈α
apparent␈α
in␈α
a␈αmoment.
␈↓Each␈αmachine␈αword␈αin␈αFS␈αis␈αdivided␈αinto␈αtwo␈αparts;␈αthe␈αleft␈αhalf␈αcontains␈αa␈αpointer␈αto␈αthe␈α␈↓αcar␈↓-part
␈↓and␈αthe␈αright␈αhalf␈αcontains␈αa␈αpointer␈αto␈αthe␈α␈↓αcdr␈↓-part.␈α The␈αpointers␈αmay␈αpoint␈αinto␈αFS␈αor␈αFWS.␈α In
␈↓more␈α
detail,␈α
consider␈α
this␈α
representation␈α
of

␈↓␈↓ ε∧␈↓α(A .(B . C)):␈↓ 











␈↓␈↓ ∧|␈↓↓A representation of ␈↓α(A .(B . C))␈↓. 

␈↓Obviously␈α⊂the␈α⊂actual␈α⊂addresses␈α⊂are␈α⊂irrelevant.␈α⊂If␈α⊂we␈α⊂replace␈α⊂the␈α⊂addresses␈α⊂with␈α⊂pointers␈α⊃to␈α⊂the
␈↓appropriate␈αcells␈αthe␈αresulting␈αdiagram␈αwill␈αcontain␈αthe␈αsame␈αinformation.␈α The␈αorigin␈αof␈αthe␈αLISP
␈↓"box␈α
notation"␈α
should␈α
be␈α
obvious.

␈↓We␈αcan␈αnow␈αdescribe␈αan␈αimplementation␈αof␈α
␈↓αeq␈↓.␈α Since␈αatoms␈αare␈αstored␈αuniquely,␈αthe␈α
predicate,␈α␈↓αeq␈↓,
␈↓need␈α∂only␈α∂check␈α∂that␈α∂its␈α∂arguments␈α∂are␈α∂both␈α∂atomic␈α∂and␈α∂both␈α∂point␈α∂to␈α∂the␈α∂same␈α∂binary␈α∂tree␈α∂or
␈↓location␈α
in␈αmemory.␈α
 In␈α
most␈αimplementations␈α
the␈α
check␈αfor␈α
atom-ness␈α
is␈αsuppressed␈α
and␈α
a␈αsimple
␈↓address␈α
comparison␈α
is␈α
made.␈α
Non-atomic␈α
sexpessions␈α
are␈α
not␈α
usually␈α
stored␈α
uniquely.␈α
Thus

␈↓␈↓ ∧\␈↓αeq[(A . B);(A . B)]␈↓ is usually false, but 

␈↓␈↓ ¬β␈↓αeq[x;x] ␈↓is usually true for any ␈↓αx␈↓. 

␈↓Please␈α⊂note␈α⊂that␈α∂we␈α⊂are␈α⊂␈↓↓not␈↓␈α∂changing␈α⊂the␈α⊂definition␈α∂of␈α⊂␈↓αeq␈↓.␈α⊂␈↓αeq␈↓␈α∂is␈α⊂still␈α⊂undefined␈α⊂for␈α∂non-atomic
␈↓arguments.␈α
 The␈α
preceding␈α
remarks␈α
deal␈α
only␈α
with␈α
the␈α
usual␈α
implementation␈α
of␈α
␈↓αeq␈↓.

␈↓The␈α
implementation␈α
of␈α
␈↓αcar␈↓␈α
and␈α
␈↓αcdr␈↓␈α
present␈α
no␈α
problem.␈α
 Simple␈α
pointer␈α
manipulation␈α
will␈α
suffice.
␈↓␈↓↓92  symbol tables␈↓ C6.␈↓









␈↓␈↓ ¬[␈↓↓The effect of ␈↓αcar␈↓. 

␈↓Finally,␈αa␈αremark␈αabout␈αconditional␈αexpressions.␈α Most␈αversions␈αof␈αLISP␈αrelax␈αthe␈αbehavior␈αof␈αthe
␈↓predicates,␈αp␈↓βi␈↓,␈αappearing␈αin␈αconditional␈αexressions␈αsuch␈αthat␈αinstead␈αof␈αtesting␈αfor␈αthe␈αvalues␈α␈↓αT␈↓␈αand
␈↓␈↓αNIL␈↓␈αwe␈αtest␈αonly␈αfor␈α␈↓αNIL␈↓␈αand␈αnon-␈↓αNIL␈↓.␈α That␈αis,␈αanything␈αother␈αthan␈α␈↓αNIL␈↓␈αsatisfies␈αthe␈αp␈↓βi␈↓.␈α This
␈↓allows␈α
such␈α
coding␈α
tricks␈α
as:
␈↓α␈↓ ¬_[x ← cdr[x] → ...]

␈↓α␈↓which is equivalent to:␈↓α

␈↓α␈↓ ¬_x ← cdr[x];
␈↓α␈↓ ¬_[not[null[x]] → ...]

␈↓(Recall that the value of "←"  is the value of the RHS.)

␈↓As␈α
with␈α
most␈α
coding␈α
tricks,␈α
they␈α
should␈α
be␈α
avoided␈α
like␈α
the␈α
plague.

␈↓Again,␈αplease␈αnote␈α
that␈αthis␈αdiscussion␈αis␈α
not␈αa␈αchange␈αto␈α
our␈αdefinition␈αof␈α
conditional␈αexpression,
␈↓but␈α
an␈α
implementation␈α
dependent␈α
feature.
␈↓␈↓↓6.1␈↓ λ≤A picture of the atom ␈↓αNIL␈↓↓     93␈↓α


␈↓␈↓ ∧␈␈↓↓6.1  A picture of the atom ␈↓αNIL␈↓↓␈↓α


␈↓We␈α
have␈α
been␈α
writing␈α
the␈α
atom␈α
␈↓αNIL␈↓␈α
as:













␈↓Where␈α
the␈α
atoms␈α
for␈α
␈↓αPNAME␈↓␈α
and␈α
␈↓αVALUE␈↓␈α
are␈α
represented␈α
as:













␈↓More␈α
realistically␈α
we␈α
should␈α
represent␈α
␈↓αNIL␈↓␈α
as:
␈↓␈↓↓94  symbol tables␈↓ 56.2␈↓


␈↓␈↓ ¬5␈↓↓6.2  A first look at ␈↓αcons␈↓␈↓α


␈↓The␈αeffect␈αof␈αthe␈α␈↓αcons␈↓␈αfunction␈αis␈α
quite␈αdifferent␈αfrom␈αthat␈αof␈αthe␈αother␈αprimitive␈α
LISP␈αfunctions.
␈↓The␈αother␈α
functions␈α(or␈α
predicates)␈αmanipulate␈α
existing␈αSexpressions,␈α
whereas␈α␈↓αcons␈↓␈α
must␈αconstruct␈α
a
␈↓new␈α
sexpression␈α
from␈α
two␈α
existing␈α
sexprs.

␈↓That␈α∞is,␈α∂given␈α∞representations␈α∂of␈α∞two␈α∞sexprs,␈α∂say␈α∞␈↓αx␈↓␈α∂and␈α∞␈↓αy,␈α∂cons[x;y]␈↓␈α∞is␈α∞to␈α∂get␈α∞a␈α∂new␈α∞cell,␈α∂put␈α∞the
␈↓representation␈α
of␈α
␈↓αx␈↓␈α
in␈α
the␈α
␈↓αcar␈↓-part␈α
of␈α
the␈α
cell␈α
and␈α
the␈α
representation␈α
of␈α
␈↓αy␈↓␈α
in␈α
the␈α
␈↓αcdr␈↓-part:

␈↓result of ␈↓αcons[x;y]␈↓


␈↓␈↓ αX␈↓ ¬_␈↓ε[~~~~~][~~~~~]

␈↓ε␈↓ αX      ␈↓rep. of ␈↓αx␈↓ ¬_␈↓ πλ     ␈↓rep. of ␈↓αy␈↓


␈↓Where␈αis␈α␈↓αcons␈↓␈αto␈αobtain␈αthese␈αnew␈αcells?␈α This␈αis␈αaccomplished␈αby␈αthe␈αfree␈αstorage␈αarea.␈αWithin␈αthe
␈↓free␈αstorage␈α(FS)␈αarea␈αresides␈αall␈αthe␈αcell␈αwhich␈αhave␈αa␈α␈↓αcar␈↓-␈αand␈α␈↓αcdr␈↓-␈αpart.␈α The␈αcells␈αwhich␈αcontain
␈↓the␈α∩actual␈α⊃p-names␈α∩we␈α∩know␈α⊃reside␈α∩in␈α∩the␈α⊃full␈α∩word␈α∩space␈α⊃(FWS).␈α∩ At␈α∩any␈α⊃point␈α∩in␈α∩a␈α⊃LISP
␈↓computation␈αthere␈αare␈αcells␈α
in␈αthe␈αFS␈αare␈α
which␈αdo␈αnot␈αcontain␈α
any␈αparts␈αof␈αthe␈αuser's␈α
computation.
␈↓In␈αparticular,␈αwhen␈αthe␈α
computation␈αis␈αbegun,␈αonly␈αthe␈α
atom␈αstructure␈αfor␈αthe␈αinitial␈α
LISP␈αsymbol
␈↓table␈α∞uses␈α∞cells␈α∞in␈α∞the␈α∞FS␈α∞area.␈α∞The␈α
remaining␈α∞FS␈α∞cells␈α∞form␈α∞a␈α∞␈↓↓free-space␈α∞list.␈↓␈α∞Whenever␈α∞a␈α
␈↓αcons␈↓
␈↓needs␈αa␈α
cell␈αthe␈αfirst␈α
cell␈αin␈α
the␈αFS␈αlist␈α
is␈αused␈αand␈α
the␈αFS␈α
list␈αis␈αset␈α
to␈αthe␈α
␈↓αcdr␈↓␈αof␈αthe␈α
FS␈αlist.␈α As␈α
the
␈↓computation␈αcontinues,␈αmore␈αand␈α
more␈αcell␈αare␈αtaken␈α
from␈αthe␈αFS␈αlist.␈α
 When␈αa␈α␈↓αcons␈↓␈αoperation␈α
asks
␈↓for␈α∂a␈α∂cell␈α∞and␈α∂the␈α∂FS␈α∞list␈α∂is␈α∂empty,␈α∂the␈α∞computation␈α∂is␈α∂suspended␈α∞and␈α∂the␈α∂␈↓↓storage␈α∂reclaimer␈↓␈α∞or
␈↓␈↓↓garbage␈α
collector␈↓␈α
is␈α
called.



␈↓␈↓ ¬8␈↓↓6.3  Garbage collection␈↓


␈↓During␈α⊂the␈α∂course␈α⊂of␈α⊂a␈α∂computation,␈α⊂contents␈α⊂of␈α∂cells␈α⊂which␈α∂were␈α⊂taken␈α⊂from␈α∂the␈α⊂FS␈α⊂list␈α∂often
␈↓become␈α
unnecessary.␈α
For␈α
example␈α
during␈α
the␈α
evaluation␈α
of␈α
something␈α
as␈α
simple␈α
as:

␈↓␈↓ βx␈↓α(CONS (QUOTE A)(QUOTE B)),␈↓ many cell are used: 

␈↓␈↓↓1.␈↓␈α∂At␈α∂least␈α∂seven␈α∂cells␈α∂are␈α∂needed␈α∂just␈α∂to␈α∂read␈α∂in␈α∂the␈α∂expression.␈α∂ If␈α∂some␈α∂of␈α∂the␈α∂atoms␈α∂are␈α∂not
␈↓present␈α
in␈α
the␈α
symbol␈α
table,␈α
more␈α
cells␈α
will␈α
be␈α
needed.

␈↓␈↓↓2.␈↓␈α
One␈α
cell␈α
will␈α
be␈α
needed␈α
to␈α
perform␈α
the␈α
␈↓αcons␈↓␈α
operation.

␈↓After␈α⊃the␈α∩computation␈α⊃is␈α∩completed,␈α⊃none␈α∩of␈α⊃the␈α⊃eight␈α∩mentioned␈α⊃cells␈α∩are␈α⊃needed.␈α∩ They␈α⊃are
␈↓␈↓↓6.3␈↓ 	∂Garbage collection     95␈↓


␈↓garbage.␈α Why␈αnot␈αsimply␈αreturn␈αthese␈α`garbage␈αcells'␈αexplicitly␈αto␈αthe␈αFS␈αlist?␈α Frequently␈αit␈αis␈αvery
␈↓difficult␈α
to␈α
know␈αjust␈α
exactly␈α
which␈αcells␈α
␈↓¬are␈↓␈α
garbage.␈α
 Indeed,␈αexperiments␈α
have␈α
been␈αperformed␈α
in
␈↓which␈α
LISP␈α
programmers␈α
were␈α
allowed␈α
to␈α
return␈α
`garbage'␈α
to␈α
the␈α
FS␈α
list␈α
themselves.␈α∞ The␈α
results
␈↓were␈α∃disasterous;␈α∃list␈α∃structure,␈α∃thought␈α∃to␈α∃be␈α∃garbage␈α∃was␈α∃returned␈α∃to␈α∃the␈α∃FS␈α∃list␈α∃by␈α∃the
␈↓programmers,␈α⊂unaware␈α⊃it␈α⊂was␈α⊂still␈α⊃being␈α⊂used␈α⊂by␈α⊃other␈α⊂computations.␈α⊂We␈α⊃will␈α⊂see␈α⊃where␈α⊂these
␈↓difficulties␈α
arise␈α
later.

␈↓Thus␈αreclamation␈αis␈αpassed␈αto␈αthe␈αLISP␈α
system.␈α The␈α␈↓αcons␈↓␈αfunction␈αand␈αits␈αFW␈α
space␈αcounterpart,
␈↓␈↓αfwcons␈↓,␈α
are␈α
the␈α
only␈α
functions␈α
allowed␈α
to␈α
mainpulate␈α
the␈α
free␈α
storage␈α
lists.␈α
 When␈α
either␈αlist␈α
becomes
␈↓empty,␈α
the␈α
garbage␈α
collector␈α
is␈α
called.

␈↓␈↓↓␈↓ βCThe fundamental assumption behind garbage collection is:␈↓ 

␈↓␈↓ αhAt␈α
any␈α
point␈α∞in␈α
a␈α
LISP␈α∞computation,␈α
all␈α
cells␈α∞which␈α
contain␈α
parts␈α∞of␈α
the
␈↓␈↓ αhcomputation␈α∪are␈α∪reachable␈α∪(by␈α∀␈↓αcar-cdr␈↓␈α∪chains)␈α∪through␈α∪a␈α∪fixed␈α∀set␈α∪of
␈↓␈↓ αhknown␈α
cells␈α
or␈α
registers.

␈↓The␈α
first␈α
phase␈α
of␈αthe␈α
garbage␈α
collector,␈α
called␈αthe␈α
␈↓↓marking␈α
phase␈↓,␈α
marks␈αall␈α
of␈α
the␈α
list␈αstructure
␈↓which␈αis␈αcurrently␈αactive␈α(reachable␈αthrough␈αthe␈αabove␈αmentioned␈αfixed␈αcells).␈αThis␈αshould␈αinclude
␈↓all␈α
the␈α∞atoms␈α
in␈α∞the␈α
symbol␈α∞table␈α
and␈α∞all␈α
the␈α
cells␈α∞reachable␈α
by␈α∞␈↓αcar-cdr␈↓␈α
chains␈α∞from␈α
any␈α∞of␈α
these
␈↓atoms.␈α
Also␈α
any␈α
partial␈α
computations␈α
which␈α
have␈αbeen␈α
generated␈α
must␈α
also␈α
be␈α
marked.␈α
Once␈αall␈α
the
␈↓active␈α
structure␈α
has␈α
been␈α
marked,␈α
we␈α
proceed␈α
to␈α
the␈α
second␈α
phase,␈α
the␈α
␈↓↓sweep␈α
phase.␈↓

␈↓Once␈αthe␈αmarking␈αhas␈αbeen␈αcompleted,␈αwe␈αgo␈αlinearly␈α(sweep)␈αthrough␈αmemory,␈αcollecting␈αall␈αthose
␈↓cells␈α
which␈α
have␈α
not␈α
been␈α
marked.␈α
 Again,␈α
the␈α
assumption␈α
is␈α
that␈α
if␈α
cells␈α
are␈α
not␈α
marked␈α
in␈αthe␈α
first
␈↓phase,␈αthen␈αthey␈αdo␈αnot␈αcontain␈αinformation␈αnecessary␈αto␈αthe␈αLISP␈αcomputation.␈α These␈αunmarked
␈↓cells␈α∞are␈α
chained␈α∞together␈α
via␈α∞their␈α
␈↓αcdr␈↓-parts␈α∞to␈α
form␈α∞a␈α
new␈α∞FS␈α
list.␈α∞The␈α
FS␈α∞pointer␈α
is␈α∞set␈α∞to␈α
the
␈↓beginning␈α
of␈α
this␈α
list;␈α
similarly,␈α
the␈α
unmarked␈α
cells␈α
in␈α
FWS␈α
comprise␈α
the␈α
new␈α
FW␈α
list.

␈↓A␈α∂more␈α∂detailed␈α⊂discussion␈α∂of␈α∂this␈α∂garbage␈α⊂collector␈α∂will␈α∂be␈α∂given␈α⊂when␈α∂examine␈α∂the␈α⊂details␈α∂of
␈↓implementation␈α⊂in␈α⊂Section␈α⊂6.9.␈α⊂ And␈α⊂a␈α⊂more␈α⊂complex␈α⊂algorithm␈α⊂is␈α⊂discussed␈α⊂in␈α⊂Section␈α⊂8.3.

␈↓Garbage␈α∂collection␈α∂appears␈α⊂to␈α∂be␈α∂a␈α⊂very␈α∂complex␈α∂and␈α∂time-consuming␈α⊂process.␈α∂Indeed␈α∂it␈α⊂is.␈α∂ As
␈↓indicated␈α
above,␈α
there␈α
are␈α
alternatives␈α
in␈α
some␈α
applications.␈α
 If␈α
the␈α
data-structure␈αmanipulations␈α
are
␈↓particularly␈α⊂simple␈α⊂then␈α⊂leaving␈α⊂storage␈α⊃management␈α⊂in␈α⊂the␈α⊂programmer's␈α⊂hands␈α⊃might␈α⊂suffice.
␈↓However,␈α
LISP␈α
generates␈α
very␈α
intertwined␈α
structures.

␈↓Perhaps␈αthere␈αis␈αanother␈αway␈αto␈αallow␈αthe␈αsystem␈αto␈αhandle␈αstorage␈αmanagement.␈α First␈αnotice␈αthat
␈↓storage␈αmanagement␈αbecomes␈αquite␈αsimple␈αif␈αthere␈αis␈αno␈αsharing␈αof␈αsublists.␈αThe␈αquestion␈αof␈αwhen
␈↓to␈α
return␈αa␈α
list␈αto␈α
the␈αfree␈α
space␈α
list␈αis␈α
easily␈αsolved.␈α
However␈αit␈α
is␈αdesirable␈α
to␈α
share␈αsubstructure
␈↓quite␈α
often.␈α
 Instead␈αof␈α
using␈α
a␈αgarbage␈α
collector,␈α
we␈αmight␈α
associate␈α
a␈αcounter,␈α
called␈α
a␈α␈↓↓reference
␈↓↓counter␈↓,␈α
with␈α
each␈α
list␈α
when␈α
it␈α
is␈α
built.␈α
In␈α
that␈α
counter␈α
we␈α
will␈α
store␈α
the␈α
number␈α
of␈α
references␈α
to
␈↓that␈α∞list.␈α∞Thus␈α∞the␈α∞counter␈α∞will␈α∞be␈α∞initialized␈α∞to␈α∞1␈α∞when␈α∞the␈α∞list␈α∞is␈α∞created.␈α∞ Whenever␈α∞the␈α∞list␈α
is
␈↓shared␈α
we␈α
increase␈α
the␈α
counter␈α
by␈α
1;␈α
whenever␈α
the␈α∞list␈α
is␈α
no␈α
longer␈α
to␈α
be␈α
shared␈α
by␈α
some␈α∞list␈α
we
␈↓decrease␈α
the␈α∞counter␈α
by␈α
1.␈α∞ When␈α
the␈α
count␈α∞goes␈α
to␈α
0␈α∞we␈α
can␈α
put␈α∞the␈α
cells␈α
of␈α∞the␈α
list␈α
in␈α∞the␈α
free
␈↓␈↓↓96  symbol tables␈↓ 56.3␈↓


␈↓space␈α
list.␈α
 One␈α
of␈α
the␈α
most␈α
glaring␈α
defects␈α
in␈α
this␈α
reference␈α
counter␈α
scheme␈α
is␈α
the␈α∞prohibition␈α
of
␈↓circular␈α
lists.␈α
Consider␈α
the␈α
following␈α
sequence:
␈↓␈↓↓1.␈↓ Manufacture a list,␈↓αx␈↓: ␈↓αx ← (B I G M O T H E R L I S T)␈↓. Reference count is 1.
␈↓␈↓↓2.␈↓ Circularize it: ␈↓αx ← circle[x];␈↓.  Reference count is now 2.
␈↓␈↓↓3.␈↓ Delete all references to ␈↓αx␈↓: ␈↓αx ← NIL␈↓. Reference count reverts to 1,
␈↓        but the list is not referred to, is not on the free space list, and has thus
␈↓        been lost to the system.

␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓I␈αThis␈αproblem␈αdeals␈αwith␈αwhat␈αis␈αknown␈αin␈αLISP␈αas␈α␈↓↓hash␈αconsing␈↓.␈α We␈αhave␈αbeen␈αstoring␈αatoms
␈↓uniquely,␈α
but␈α
it␈α
should␈α
be␈α
clear␈α
from␈α∞the␈α
behavior␈α
of␈α
␈↓αcons␈↓␈α
that␈α
non-atomic␈α
sexprs␈α
are␈α∞␈↓↓not␈↓␈α
stored
␈↓uniquely.␈α∀ Certainly␈α∀storing␈α∀single␈α∃copies␈α∀of␈α∀any␈α∀sexpr␈α∃would␈α∀save␈α∀space.␈α∀For␈α∃example,␈α∀the
␈↓non-atomic␈α∂structure␈α∂of␈α∂␈↓α((A␈α∂.␈α⊂B).(A␈α∂.␈α∂B))␈↓␈α∂could␈α∂be␈α⊂represented␈α∂with␈α∂two␈α∂cells␈α∂rather␈α⊂than␈α∂three.
␈↓Unique␈αstorage␈αis␈αnot␈αwithout␈α
its␈αdifficulties.␈αWhat␈αproblems␈αdo␈α
you␈αforsee␈αin␈αimplementing␈αsuch␈α
a
␈↓scheme?

␈↓II␈αWe␈α
said␈αon␈α
page␈α90␈α
that␈αmany␈α
LISP␈αcomputations␈α
generate␈αlist␈α
structure␈αrather␈α
than␈αtrue␈α
binary
␈↓trees?␈α
Give␈α
an␈α
example.

␈↓II␈αCan␈α
you␈αwrite␈α
a␈αLISP␈α
function␈α␈↓αcircle␈α
<=␈αλ[[x]␈α
...␈α␈↓␈α
which␈αwill␈α
take␈αa␈α
list␈α␈↓αx␈↓␈α
and␈αmake␈α
it␈αcircular.
␈↓Thus:

␈↓ε␈↓ α8[~~~~~~~[~~~]␈↓ ¬_[~~~]~~~]␈↓ π([~~]~~~]␈↓ 	H[~~~~]~~~]
␈↓ε␈↓α␈↓ α8NOTHING␈↓ ¬_ CAN␈↓ π( GO␈↓ 	H WRONG

␈↓This␈α⊃list␈α⊂is␈α⊃circular␈α⊂on␈α⊃its␈α⊂"last␈α⊃two"␈α⊃elements.␈α⊂ Needless␈α⊃to␈α⊂say␈α⊃printing␈α⊂such␈α⊃structures␈α⊃is␈α⊂not
␈↓appreciated.



␈↓␈↓ ¬T␈↓↓6.4  ␈↓αread␈↓↓ and ␈↓αprint␈↓↓␈↓α


␈↓When␈α
you␈α
begin␈α
to␈α
implement␈α
LISP␈α
you␈α
find␈α
that␈α
a␈α
very␈α
significant␈α
part␈α
of␈α
LISP␈α
can␈α
be␈αwritten␈α
in
␈↓LISP.␈α
 We␈α
have␈α
already␈α
seen␈α
that␈α
the␈α
evaluation␈α
process␈α
is␈α
expressible␈α
this␈α
way.

␈↓Here␈α
we␈αwill␈α
see␈αthat␈α
the␈αmajority␈α
of␈αthe␈α
I/O␈αroutines␈α
can␈αbe␈α
written␈αas␈α
LISP␈αfunctions␈α
calling␈αa
␈↓very␈αfew␈αprimitive␈αroutines.␈α The␈αprimitive␈αroutines␈αcan␈αalso␈αbe␈αdescribed␈αin␈α`pseudo-LISP'.␈α That
␈↓is,␈αwe␈αwill␈αgive␈αa␈αLISP-like␈αdescription␈αof␈αthem,␈αthough␈αthey␈αwould␈αnormally␈αbe␈αcoded␈αin␈αmachine
␈↓language.

␈↓The␈α
primitive␈α
functions␈α
are␈α
␈↓αratom␈↓␈α
(read␈α
an␈α
atom)␈α
and␈α
␈↓αprin1␈↓.
␈↓␈↓↓6.4␈↓ 	J␈↓αread␈↓↓ and ␈↓αprint␈↓↓     97␈↓α



␈↓␈↓αratom[␈α]␈↓␈αis␈αa␈αfunction␈αof␈αno␈αarguments.␈αIt␈α
reads␈αthe␈αnext␈αatom␈αor␈αspecial␈αcharacter␈α(left␈αparen,␈α
right
␈↓␈↓ αhparen␈αor␈αdot).␈α It␈α
looks␈αup␈αthat␈αobject␈α
in␈αthe␈αsymbol␈αtable␈α
and␈αreturns␈αa␈αpointer␈αto␈α
that
␈↓␈↓ αhsymbol␈α∞table␈α
entry.␈α∞ If␈α∞no␈α
entry␈α∞is␈α
found␈α∞an␈α∞appropriate␈α
entry␈α∞is␈α
made.␈α∞ ␈↓αratom␈↓␈α∞is␈α
the
␈↓␈↓ αhLISP␈α⊂scanner.␈α⊂␈↓αratom␈↓␈α⊂flushes␈α⊂spaces␈α⊃and␈α⊂commas,␈α⊂using␈α⊂them␈α⊂only␈α⊂for␈α⊃delimiters.␈α⊂It
␈↓␈↓ αhreturns␈α
only␈α
atoms␈α
or␈α
special␈α
characters␈α
to␈α
␈↓αread␈↓,␈α
the␈α
LISP␈α
parser.

␈↓␈↓αprin1[x]␈↓␈αis␈αa␈αfunction␈αof␈αone␈αargument␈αexpecting␈αan␈αatom,␈αleft␈αparen␈αright␈αparen,␈αblank,␈αor␈αdot␈αas
␈↓␈↓ αhthe␈α
value␈α
of␈α
its␈α
argument.␈α
 It␈α
will␈α
print␈α
the␈α
p-name␈α
of␈α
that␈α
object␈α
on␈α
the␈α
output␈α
device.

␈↓To␈αmake␈αlife␈αsimpler␈αwe␈αneed␈αto␈αbe␈αable␈αto␈αrefer␈αto␈αatoms␈αwhose␈αvalues␈αare␈αthe␈αcharacters␈α"␈↓α)␈↓",␈α"␈↓α(␈↓",
␈↓".",␈αand␈α
"␈α"(blank).␈α
 We␈αwill␈αassume␈α
that␈α␈↓αrpar␈↓,␈α
␈↓αlpar␈↓,␈α␈↓αperiod␈↓,␈αand␈α
␈↓αblank␈↓␈αare␈α
such␈αatoms.␈α For␈α
example,
␈↓if␈α
the␈α
next␈α
input␈α
character␈α
is␈α
"("␈α
then

␈↓␈↓ αP␈↓αeq[ratom[ ];lpar]␈↓ is true (and the input pointer is moved to the next character!). 

␈↓␈↓αprin1[period]␈↓␈α
will␈α
have␈α
the␈α
effect␈α
of␈α
printing␈α
a␈α
␈↓↓"."␈↓␈α
on␈α
the␈α
output␈α
device.

␈↓We␈αwill␈αdiscuss␈αthe␈αstructure␈αof␈α␈↓αratom␈↓␈αand␈α␈↓αprin1␈↓␈αin␈αa␈αmoment.␈α In␈αthe␈αmeantime␈αhere␈αare␈α␈↓αread␈↓␈αand
␈↓␈↓αprint␈↓:

␈↓αread <=λ[[ ]prog[[j]
␈↓α␈↓ βHj ← ratom[ ];
␈↓α␈↓ βH[eq[j;lpar] → return[read1[ ]];
␈↓α␈↓ βH or[eq[j;rpar];eq[j;period]] → LOSEBIG1;
␈↓α␈↓ βH T → return[j]]]]

␈↓αread1 <= λ[[ ]prog[[j;k]
␈↓α␈↓ βH␈↓ βxj ← ratom[ ];
␈↓α␈↓ βH␈↓ βx[eq[j;lpar] → return[cons[read1[ ];read1[ ]]];
␈↓α␈↓ βH␈↓ βx eq[j;rpar] → return[NIL];
␈↓α␈↓ βH␈↓ βx eq[j;period] → go[rd];
␈↓α␈↓ βH␈↓ βx T → return[cons[j;read1[ ]]] ];
␈↓α␈↓ βHrd␈↓ βxk ← ratom[ ];
␈↓α␈↓ βH␈↓ βx[eq[k;lpar] → k ← read1[ ];
␈↓α␈↓ βH␈↓ βx or[eq[k;rpar];eq[k;period]] → LOSEBIG2];
␈↓α␈↓ βH␈↓ βxj ← ratom[ ];
␈↓α␈↓ βH␈↓ βx[eq[j;rpar] → return[k];
␈↓α␈↓ βH␈↓ βx T → LOSEBIG3]  ]]

␈↓Here's␈α
␈↓αprint␈↓␈α
and␈α
friends.␈α
␈↓αterpri␈↓␈α
gets␈α
a␈α
new␈α
output␈α
line.␈α
 (note:␈α
the␈α
value␈α
of␈α
␈↓αprint[x]␈↓␈α
is␈α
␈↓αx␈↓.)

␈↓αprint <= λ[[x]prog[[ ]
␈↓α␈↓ βHprin0[x];
␈↓α␈↓ βHterpri[ ];
␈↓α␈↓ βHreturn[x]]]
␈↓␈↓↓98  symbol tables␈↓ 26.4␈↓


␈↓αprin0 <= λ[[x]prog[[j]
␈↓α␈↓ βH␈↓ βx[atom[x] → return[prin1[x]]];
␈↓α␈↓ βH␈↓ βxj ← x;
␈↓α␈↓ βHa3␈↓ βxprin0[car[j]];
␈↓α␈↓ βH␈↓ βx[null[cdr[j]] → go[a6]]
␈↓α␈↓ βH␈↓ βxprin1[blank];
␈↓α␈↓ βH␈↓ βx[atom[cdr[j]] → go [p1]];
␈↓α␈↓ βH␈↓ βxj ← cdr[j];
␈↓α␈↓ βH␈↓ βxgo[a3];
␈↓α␈↓ βHp1␈↓ βxprin1[period];
␈↓α␈↓ βH␈↓ βxprin1[blank];
␈↓α␈↓ βH␈↓ βxprin1[cdr[j]];
␈↓α␈↓ βHa6␈↓ βxprin1[rpar];
␈↓α␈↓ βH␈↓ βxreturn[x] ]]

␈↓So␈α
far␈α
we␈α
have␈α
thrown␈α
all␈α
the␈α
I/O␈α
back␈α
on␈α
␈↓αratom␈↓␈α
and␈α
␈↓αprin1␈↓.␈α
 Clearly␈α
␈↓αratom␈↓␈α
will␈α
be␈α
more␈α
interesting.
␈↓All␈α
␈↓αprin1␈↓␈αneed␈α
do␈α
is␈αget␈α
the␈α
p-name␈αand␈α
print␈α
it.␈α ␈↓αratom␈↓␈α
needs␈α
to␈αsearch␈α
the␈α
symbol␈αtable␈α
(efficiently
␈↓hopefully),␈α∞and␈α∞if␈α∞the␈α∞atom␈α∞is␈α
not␈α∞found,␈α∞add␈α∞it␈α∞to␈α∞the␈α∞table.␈α
 All␈α∞␈↓αratom␈↓␈α∞has␈α∞to␈α∞work␈α∞with␈α∞is␈α
the
␈↓actual␈αcharacter␈αstring␈α(called␈α␈↓αchr-str␈↓␈αin␈αthe␈αfuture)␈αwhich␈αwill␈αbe␈αthe␈αp-name␈αof␈αsome␈αatom.␈α What
␈↓␈↓αratom␈↓␈α
could␈αdo␈α
is␈α
look␈αat␈α
the␈α
p-name␈αof␈α
each␈αatom␈α
currently␈α
in␈αthe␈α
symbol␈α
table;␈αwhen␈α
it␈α
finds␈αa
␈↓match␈α⊂it␈α⊂returns␈α⊂a␈α⊂pointer␈α⊂to␈α⊂the␈α⊂beginning␈α⊂of␈α⊂that␈α⊂atom.␈α⊂(Notice␈α⊂this␈α⊂is␈α⊂essentially␈α⊂the␈α⊂search
␈↓scheme␈α∂of␈α∂␈↓αassoc␈↓.)␈α∂If␈α∂the␈α∂appropriate␈α∞atom␈α∂is␈α∂not␈α∂found␈α∂it␈α∂can␈α∞build␈α∂a␈α∂new␈α∂one␈α∂consiting␈α∂of␈α∞the
␈↓p-name,␈α∪add␈α∩it␈α∪to␈α∪the␈α∩table,␈α∪and␈α∪return␈α∩a␈α∪pointer␈α∪to␈α∩this␈α∪new␈α∪atom.␈α∩ This␈α∪would␈α∪have␈α∩the
␈↓appropriate␈α
effect;␈α
each␈α
atom␈αwould␈α
be␈α
stored␈α
uniquely,␈α
but␈αthe␈α
efficiency␈α
would␈α
leave␈αsomething␈α
to
␈↓be␈α
desired.

␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓*****␈α
non-recursive␈α
reader...better␈α
worse***

␈↓***␈α
slashifying␈α
***



␈↓␈↓ ¬{␈↓↓6.5  Hashing␈↓


␈↓Symbol␈α⊃table␈α⊂lookup␈α⊃is␈α⊃exactly␈α⊂the␈α⊃problem␈α⊂of␈α⊃looking␈α⊃up␈α⊂words␈α⊃in␈α⊂a␈α⊃dictionary.␈α⊃ The␈α⊂above
␈↓scheme␈α
of␈α
␈↓αassoc␈↓␈α
(linear␈α
search)␈α
is␈α
analogous␈α
to␈α
a␈α
person␈α
beginning␈α
at␈α
page␈α
one␈α
of␈α
the␈α
dictionary␈α
and
␈↓proceeding␈αlinearly␈α(page-by-page␈α
and␈αword-by-word)␈αthrough␈α
the␈αbook␈αuntil␈α
he␈αfound␈αthe␈αword␈α
in
␈↓question.␈α
 Truly␈α
a␈α
losing␈α
scheme.␈α
What␈α
we␈α
normally␈α
do␈α
is␈α
look␈α
at␈α
the␈α
first␈α
character␈α
of␈α
word␈αand␈α
go
␈↓immediately␈αto␈αthe␈αsubsection␈αof␈αthe␈αdictionary␈α
which␈αhas␈αthe␈αwords␈αbeginning␈αwith␈αthat␈α
character.
␈↓We␈α∞know␈α∂that␈α∞if␈α∞we␈α∂cannot␈α∞find␈α∂the␈α∞defintion␈α∞of␈α∂our␈α∞word␈α∂in␈α∞that␈α∞subsection␈α∂we␈α∞need␈α∂look␈α∞no
␈↓further␈α∀in␈α∪the␈α∀book.␈α∪ Usually␈α∀we␈α∪delimit␈α∀our␈α∪search␈α∀even␈α∪further␈α∀by␈α∪keying␈α∀on␈α∪subsequent
␈↓characters␈αin␈αthe␈αword.␈α Finally␈αwe␈αmay␈αresort␈αto␈αlinear␈αsearch␈αto␈αpin␈αdown␈αthe␈αword␈αon␈αa␈αspecific
␈↓␈↓↓6.5␈↓ 
↔Hashing     99␈↓


␈↓page␈αor␈αcolumn.␈α What␈αwe␈αwant␈αis␈αa␈αsimilar␈αscheme␈αfor␈αthe␈αmachine.␈α We␈αmight␈αin␈αfact␈αmimic␈αthe
␈↓dictionary␈α
search,␈α
subdividing␈α
our␈α
table␈α
into␈α
26␈α
subsections.␈α
 We␈α
can␈α
do␈α
better.

␈↓Since␈α
it␈α
is␈α
the␈αmachine␈α
which␈α
will␈α
subdivide␈αand␈α
index␈α
into␈α
the␈α
symbol␈αtable,␈α
we␈α
can␈α
pick␈αa␈α
scheme
␈↓which␈αis␈αcomputationally␈αconvenient␈αfor␈αthe␈αmachine.␈αBesides␈αbeing␈αconvenient,␈αthe␈αscheme␈αshould
␈↓result␈α∂in␈α∂rather␈α∂even␈α∂distribution␈α∂of␈α∂atoms␈α⊂in␈α∂the␈α∂subsections.␈α∂Obviously␈α∂if␈α∂the␈α∂majority␈α⊂of␈α∂the
␈↓atoms␈αend␈α
up␈αin␈α
the␈αsame␈α
partition␈αof␈α
the␈αtable␈α
we␈αwill␈α
have␈αgained␈α
little␈αtowards␈α
improving␈αthe
␈↓search␈α
efficiency.␈α
 Now␈α
for␈α
terminology.␈α
 Each␈α
of␈α
the␈α
partitions␈α
or␈α
subdivisions␈α
of␈α
the␈α
table␈αis␈α
called
␈↓a␈α
␈↓↓bucket␈↓.␈α
 Each␈α
atom␈α∞will␈α
appear␈α
in␈α
at␈α
most␈α∞one␈α
bucket.␈α
 The␈α
computational␈α
scheme␈α∞or␈α
function
␈↓used␈α∞to␈α∞determine␈α∂which␈α∞bucket␈α∞a␈α∞particular␈α∂atom␈α∞belongs␈α∞in␈α∞is␈α∂called␈α∞a␈α∞␈↓↓hashing␈α∂function␈↓.␈α∞ All
␈↓␈↓αratom␈↓␈α∞has␈α
to␈α∞work␈α
with␈α∞is␈α
␈↓αchr-str␈↓,␈α∞the␈α
encoding␈α∞of␈α
the␈α∞actual␈α
name␈α∞of␈α
the␈α∞atom.␈α
 So␈α∞the␈α
hashing
␈↓function␈αwill␈αuse␈α␈↓αchr-str␈↓␈αto␈αdetermine␈αwhich␈α
bucket␈αto␈αexamine.␈α If␈αthe␈αatom␈αwith␈α
PNAME␈α␈↓αchr-str␈↓
␈↓does␈α
not␈α
appear␈αin␈α
that␈α
bucket␈α
we␈αare␈α
assured␈α
that␈αit␈α
does␈α
not␈α
appear␈αanywhere␈α
in␈α
the␈α
table.␈α In
␈↓this␈αcase␈αwe␈αmake␈αup␈αthe␈αminimal␈αtable␈αentry␈α-- atom indicator,␈α␈↓αPNAME␈↓,␈αp-name␈α
structure --␈αand
␈↓add␈α
it␈α
to␈α
that␈α
bucket.

␈↓There␈α
are␈α
lots␈α
of␈α
hashing␈α
functions.␈α
Here's␈α
one:

␈↓␈↓↓1.␈↓␈α
Assume␈α
that␈α
we␈α
have␈α
N+1␈α
buckets,␈α
numbered␈α
0,␈α
1,␈α
2␈α
...␈α
N.

␈↓␈↓↓2.␈↓␈α
Take␈α
the␈α
representation␈α
of␈α
␈↓αchr-str␈↓␈α
(it's␈α
a␈α
number)␈α
and␈α
divide␈α
it␈α
by␈α
N+1.

␈↓␈↓↓3.␈↓␈α
Look␈α
at␈α
the␈α
remainder.␈α
 It's␈α
a␈α
number␈α
between␈α
0␈α
and␈α
N.

␈↓␈↓↓4.␈↓␈α
Use␈α
that␈α
remainder␈α
as␈α
the␈α
index␈α
to␈α
the␈α
appropriate␈α
bucket.

␈↓This␈αis␈α
a␈αscheme␈α
frequently␈αused␈α
in␈αLISP.␈α Given␈α
the␈αbucket␈α
number,␈αwe␈α
then␈αrun␈α
down␈αthe␈αlist␈α
of
␈↓atoms␈αin␈αthat␈αbucket,␈αcomparing␈αprint-names␈αagainst␈α␈↓αchr-str␈↓.␈α If␈αthe␈αatom␈αis␈αnot␈αfound,␈α␈↓αcons␈↓-up␈αthe
␈↓atom␈α
and␈α
stick␈α
it␈α
in␈α
the␈α
bucket.

␈↓There␈αis␈αa␈αlot␈αof␈α
mystery␈αand␈αhistory␈αabout␈αhashing␈αand␈α
other␈αmeans␈αof␈αsearching␈αand␈α
storing␈αin
␈↓symbols.␈α
References␈α
are␈α
given␈α
in␈α
the␈α
bibliography.

␈↓In␈α
review,␈α
here␈α
is␈α
the␈α
structure␈α
of␈α
the␈α
LISP␈α
input␈α
mechanism:

␈↓␈↓↓1.␈↓␈α
␈↓αread␈↓,␈αthe␈α
LISP␈α
parser,␈αbuilds␈α
a␈αlist-structure␈α
representation␈α
of␈αthe␈α
input␈α
string.␈α ␈↓αread␈↓␈α
is␈αdefined␈α
in
␈↓terms␈α
of␈α
␈↓αratom␈↓.

␈↓␈↓↓2.␈↓␈α∂␈↓αratom␈↓,␈α∂the␈α∂LISP␈α∂scanner,␈α∂builds␈α∂atoms␈α∞and␈α∂special␈α∂characters␈α∂from␈α∂the␈α∂input.␈α∂ It␈α∂searchs␈α∞and
␈↓increments␈α
the␈α
LISP␈α
symbol␈α
table.

␈↓␈↓↓3.␈↓␈α
The␈αLISP␈α
symbol␈αtable,␈α
usually␈αcalled␈α
␈↓αOBLIST␈↓␈α(for␈α
␈↓↓object␈αlist␈↓),␈α
is␈αa␈α
list␈αof␈α
buckets.␈α Each␈α
bucket
␈↓is␈αa␈αlist␈αof␈αthe␈αatoms␈αwhich␈α`hash'␈αto␈αthat␈αbucket.␈α We␈αactually␈αrepresent␈αthe␈αobject␈αlist␈αas␈αan␈αarray
␈↓named␈α∞␈↓αoblist␈↓.␈α
 This␈α∞way␈α∞the␈α
hash␈α∞number␈α
will␈α∞give␈α∞us␈α
the␈α∞array␈α
subscript␈α∞and␈α∞we␈α
can␈α∞go␈α∞to␈α
the
␈↓right␈α
bucket␈α
immediately.␈α
 We␈α
won't␈α∞have␈α
to␈α
go␈α
"cdr-ing"␈α
down␈α
the␈α∞object␈α
list␈α
to␈α
get␈α
to␈α∞the␈α
right
␈↓bucket.␈α
 See␈α
figure␈α
on␈α
next␈α
page.
␈↓␈↓↓100  symbol tables␈↓ 46.5␈↓


␈↓Here␈α
is␈α
a␈α
`pseudo-LISP'␈α
version␈α
of␈α
␈↓αratom␈↓:

␈↓␈↓αhash␈↓␈↓ βHis␈α
a␈α
function␈α
returning␈α
the␈α
bucket␈α
number␈α
of␈α
its␈α
argument.

␈↓␈↓αinsert␈↓␈↓ βHis␈α
a␈α
function␈α
to␈α
build␈α
the␈α
atom␈α
and␈α
insert␈α
it␈α
into␈α
to␈α
bucket.

␈↓␈↓αright-one␈↓␈↓ βHis␈α
a␈α
predicate␈α
used␈α
to␈α
check␈α
if␈α
an␈α
atom␈α
has␈α
the␈α
right␈α
print-name.

␈↓αratom <= λ[[ ]prog[[z;i;bucket]
␈↓α␈↓ βH␈↓ βxi ← hash[chr-str];
␈↓α␈↓ βH␈↓ βxbucket ← oblist[i];
␈↓α␈↓ βHa␈↓ βx[null[bucket] → return[insert[chr-str]]];
␈↓α␈↓ βH␈↓ βxz ← get[car[bucket];PNAME];
␈↓α␈↓ βH␈↓ βx[right-one[z;chr-str] → return[car[bucket]]];
␈↓α␈↓ βH␈↓ βxbucket ← cdr[bucket];
␈↓α␈↓ βH␈↓ βxgo[a]]]

␈↓␈↓αget␈↓␈α
is␈α
a␈α
LISP␈α
system␈α
function.␈α
It␈α
is␈α
described␈α
on␈α
page␈α
123.
␈↓␈↓↓6.6␈↓ ¬zA review of the structure of  the LISP machine     101␈↓


␈↓␈↓ βu␈↓↓6.6  A review of the structure of  the LISP machine␈↓



␈↓␈↓ αX_______________________________________
␈↓␈↓ αX␈↓ βx␈↓αeval␈↓ and friends
␈↓␈↓ αX␈↓ βx␈↓αread␈↓ and ␈↓αprint␈↓
␈↓␈↓ αX␈↓ βxthe garbage collector
␈↓␈↓ αX␈↓ βxthe base registers for marking
␈↓␈↓ αX␈↓ βx  FS pointer
␈↓␈↓ αX␈↓ βx  FWS pointer
␈↓␈↓ αX␈↓ βx  symbol table(␈↓αOBLIST␈↓) pointer
␈↓␈↓ αX␈↓ βx  registers for partial results

␈↓␈↓ αX_______________________________________
␈↓␈↓ αX␈↓ βxFree space
␈↓␈↓ αX␈↓ βx the free space list
␈↓␈↓ αX␈↓ βx those parts of sexprs containing
␈↓␈↓ αX␈↓ βx   ␈↓αcar␈↓- and ␈↓αcdr␈↓-parts.

␈↓␈↓ αX_______________________________________
␈↓␈↓ αX␈↓ βxFull word space
␈↓␈↓ αX␈↓ βx  the full word space list
␈↓␈↓ αX␈↓ βx  atom print names
␈↓␈↓ αX␈↓ βx  numbers
␈↓␈↓ αX_______________________________________
␈↓␈↓↓102  symbol tables␈↓ 56.7␈↓


␈↓␈↓ ¬≤␈↓↓6.7  More on symbol tables␈↓


␈↓␈↓ βB␈↓↓Subtitled: But my Fortran program doesn't do all this crap!␈↓ 

␈↓It␈αis␈αquite␈αtrue␈αthat␈αa␈αrunning␈αFortran,␈αPL/1,␈αor␈αAlgol␈αprogram␈αis␈αfar␈αless␈αcomplicated␈αas␈αfar␈αas␈αits
␈↓symbol␈α⊂accessing␈α⊂mechanisms␈α⊂are␈α⊂concerned.␈α⊂ In␈α∂Fortran␈α⊂there␈α⊂is␈α⊂a␈α⊂simple␈α⊂relationship␈α∂between
␈↓variables␈α∪and␈α∪memory␈α∪locations␈α∀which␈α∪will␈α∪contain␈α∪their␈α∪values;␈α∀in␈α∪Algol,␈α∪there␈α∪is␈α∀a␈α∪simple
␈↓relationship␈α
between␈α
variables␈α
and␈α
positions␈α
in␈α
the␈α
run-time␈α
stack.

␈↓In␈α
LISP,␈α
both␈α
the␈α
quality␈α
and␈α
the␈αquantity␈α
of␈α
variables␈α
can␈α
change.␈α
 Arbitrary␈α
properties␈α
can␈αbe
␈↓associated␈α⊗with␈α⊗atoms␈α∃at␈α⊗run-time.␈α⊗ Indeed,␈α∃the␈α⊗symbol␈α⊗table␈α∃mechanism␈α⊗of␈α⊗LISP␈α⊗is␈α∃more
␈↓reminiscent␈α⊂of␈α⊃that␈α⊂associated␈α⊂with␈α⊃the␈α⊂Fortran␈α⊃or␈α⊂Algol␈α⊂compiler.␈α⊃ For␈α⊂these␈α⊃less␈α⊂sophisticated
␈↓languages␈α
it␈α
is␈α
the␈α
compiler␈α
which␈αperforms␈α
the␈α
mapping␈α
from␈α
source␈α
language␈α
to␈αrunning␈α
machine
␈↓code.␈α∂ It␈α∂is␈α∂the␈α∂compiler's␈α∂responsibility␈α∞to␈α∂discover␈α∂the␈α∂properties␈α∂associated␈α∂with␈α∂each␈α∞variable.
␈↓The␈α∂compiler␈α∂can␈α∂do␈α∞this␈α∂because␈α∂the␈α∂semantics␈α∞of␈α∂the␈α∂language␈α∂is␈α∞such␈α∂that␈α∂at␈α∂compile␈α∂all␈α∞(or
␈↓almost␈α
all)␈α
properties␈α
of␈αthe␈α
variables␈α
are␈α
known.␈α This␈α
is␈α
not␈α
true␈αfor␈α
LISP.␈α
 In␈α
general␈αyou␈α
cannot
␈↓tell␈αuntil␈αrun␈αtime␈αwhat␈αthe␈αattributes␈αof␈αa␈αparticular␈αatom␈αare.␈α The␈αsituation␈αis␈αreally␈αeven␈αworse
␈↓than␈α∂this.␈α⊂ Since␈α∂programs␈α⊂and␈α∂data␈α⊂are␈α∂indistinguishable,␈α∂we␈α⊂can␈α∂␈↓αcons␈↓-up␈α⊂a␈α∂list,␈α⊂assign␈α∂it␈α⊂to␈α∂a
␈↓variable,␈α
then␈α
turn␈α
around␈α
and␈α
use␈α
that␈α
variable␈α
as␈α
a␈α
function.␈α
 Try␈α
that␈α
in␈α
Fortran.

␈↓Now␈αthe␈αworld␈αisn't␈αall␈αthis␈αgrim.␈α There␈α
are␈αLISP␈αcompilers␈α(written␈αin␈αLISP␈αnaturally).␈αThey␈α
can
␈↓make␈αmany␈αdecisions␈α
at␈αcompile␈αtime␈αabout␈α
the␈αproperties␈αof␈αvariables␈α
But␈αin␈αgeneral␈αthe␈α
compiled
␈↓code␈α∂will␈α∂be␈α∂interspersed␈α∂with␈α∂calls␈α∂on␈α∂␈↓αeval␈↓.␈α⊂ That␈α∂is,␈α∂␈↓αeval␈↓␈α∂will␈α∂have␈α∂to␈α∂make␈α∂some␈α⊂decisions␈α∂at
␈↓runtime,␈α⊃because␈α⊃the␈α⊂compiler␈α⊃just␈α⊃doesn't␈α⊃know␈α⊂what␈α⊃to␈α⊃do.␈α⊂ This␈α⊃implies␈α⊃that␈α⊃compiled␈α⊂and
␈↓interpreted␈αcode␈αmust␈αbe␈αable␈αto␈αcommunicate␈αwith␈αeach␈αother.␈α If␈αa␈αpiece␈αof␈αcompiled␈αcode␈αcalls␈αa
␈↓λ-expression␈αor␈αconversely,␈αthe␈αright␈αthing␈αmust␈αhappen.␈α The␈αexecution␈αof␈αthe␈αprogram␈αshould␈αbe
␈↓totally␈α∞transparent␈α
as␈α∞to␈α
whether␈α∞any,␈α
or␈α∞all,␈α
or␈α∞none␈α
of␈α∞the␈α
functions␈α∞are␈α
compiled.␈α∞ This␈α
means
␈↓that␈αthe␈αcalling␈αsequences␈αfor␈αboth␈αkinds␈αof␈αfunctions␈αmust␈αbe␈αcompatible.␈αLess␈αobvious␈αand␈αby␈αfar
␈↓more␈α
troublesome,␈α
is␈α
the␈α
communication␈α
of␈α
global␈α
values␈α
between␈α
functions.



␈↓␈↓ ¬D␈↓↓6.8  Global Variables␈↓


␈↓A␈αvariable␈αused␈αglobally␈α(or␈αfree)␈αin␈αa␈αfunction␈α(or␈α␈↓αprog␈↓)␈αis␈αone␈αwhich␈αdoes␈αnot␈αappear␈αin␈αthe␈α
λ-list
␈↓or␈α
in␈α
the␈α
list␈α
of␈α
␈↓αprog␈↓-variables.

␈↓For␈α
example␈α
in:

␈↓␈↓ ¬<␈↓αλ[[x]*[x;y]], y␈↓ is global. 

␈↓Communication␈α⊃of␈α⊃global␈α⊃variables␈α⊃between␈α⊃interpreted␈α⊃functions␈α⊃is␈α⊃not␈α⊃problematic:␈α∩just␈α⊃look
␈↓down␈α
the␈α
atom␈α
structure␈α
for␈α
the␈α
␈↓αVALUE␈↓-cell.␈α
Handling␈α
of␈α
global␈α
variables␈α
by␈α
compiled␈αfunctions
␈↓␈↓↓6.8␈↓ 	~Global Variables     103␈↓


␈↓requires␈α∞some␈α∂care.␈α∞ The␈α∞␈↓αVALUE␈↓-cell␈α∂mechanism␈α∞is␈α∞designed␈α∂to␈α∞accomplish␈α∞this.␈α∂ Essentially,␈α∞the
␈↓␈↓αVALUE␈↓-cell␈α
will␈α∞␈↓↓always␈↓␈α
contain␈α
the␈α∞current␈α
value␈α
of␈α∞its␈α
variable.␈α
The␈α∞compiler␈α
then␈α
needs␈α∞to␈α
be
␈↓able␈α
to␈α
find␈α
this␈α
cell␈α
and␈α
generate␈α
efficient␈α
code␈α
to␈α
manipulate␈α
its␈α
contents.

␈↓What␈α
we␈α
will␈α
do␈α
now␈α
is:

␈↓␈↓↓1.␈↓␈α
Introduce␈α
a␈αgraphical␈α
language,␈α
based␈α
on␈αAMBIT/G,␈α
to␈α
describe␈α
many␈αof␈α
the␈α
operations␈αof␈α
LISP
␈↓(Section␈α16.9).

␈↓␈↓↓2.␈↓␈αDescribe␈αparts␈αof␈αthe␈αdynamics␈αof␈αevaluation␈αusing␈αthe␈αContour␈αModel␈α(Section␈α6.12).

␈↓␈↓↓3.␈↓␈αDescribe␈α
the␈αmechanism␈α
for␈αuser-defined␈αspecial␈α
forms,␈αand␈α
another␈αdefining␈α
mechanism␈αcalled
␈↓macros␈α⊗(Section␈α⊗6.13).

␈↓␈↓↓4.␈↓␈α
Give␈α
a␈α
new␈α
improved␈α
definition␈α
of␈α∞␈↓αeval␈↓␈α
and␈α
Co.␈α
which␈α
uses␈α
the␈α
new␈α
symbol␈α∞table␈α
mechanism
␈↓and␈αevaluates␈αforms,␈αspecial␈αforms␈αand␈αmacros.␈α Show␈αhow␈αthe␈αbinding␈αmechanism,␈αcalled␈α␈↓↓shallow
␈↓↓binding␈↓,␈α∩operates␈α∩(Section␈α∩6.15).

␈↓␈↓↓5.␈↓␈α
Introduce␈α
a␈α
simple␈α
stack␈α
machine,␈α
␈↓	SM␈↓,␈α
mapping␈α
the␈α
graphical␈α
descriptions␈α
onto␈α∞a␈α
PDP-10-like
␈↓machine␈α⊗(Section␈α⊗6.18).

␈↓␈↓↓6.␈↓␈αWrite␈αa␈αLISP␈αfunction␈αwhich␈αwill␈αoperate␈αas␈αa␈αcompiler,␈αtranslating␈αthe␈αSexpr␈αrepresentations␈α
of
␈↓LISP␈α∞functions␈α∂into␈α∞`equivalent'␈α∞sequences␈α∂of␈α∞␈↓	SM␈↓␈α∞code␈α∂(Section␈α∞7.6).

␈↓␈↓↓7.␈↓␈αThen␈αwe␈αcan␈αsee␈αhow␈αthe␈αproblem␈αof␈αglobal␈αvariables␈αis␈αhandled␈α(Section␈α7.11).

␈↓In␈α∂parallel␈α∞to␈α∂␈↓↓this␈↓␈α∞development␈α∂will␈α∂run␈α∞a␈α∂presentation␈α∞of␈α∂an␈α∞alternative␈α∂binding␈α∂strategy␈α∞called
␈↓␈↓↓deep␈α∂binding␈↓.␈α∂We␈α∂will␈α∂also␈α∂present␈α∂an␈α∞alternative␈α∂calling␈α∂structure␈α∂which␈α∂works␈α∂well␈α∂with␈α∞deep
␈↓bindings␈α
and␈α
is␈α
not␈α
so␈α
hardware␈α
oriented␈α
as␈α
its␈α
competitor.␈α
 These␈α
alternatives␈α
are␈α
perhaps␈αmore
␈↓intuitive␈α
implementations␈αour␈α
original␈α
a-list␈αversion␈α
of␈α
␈↓αeval␈↓␈αthan␈α
is␈α
the␈αshallow␈α
binding␈αscheme.␈α
We
␈↓will␈α
contrast␈α
their␈α
relative␈α
efficiencies.



␈↓␈↓ ¬q␈↓↓6.9  AMBIT/G␈↓


␈↓AMBIT/G␈↓π 10␈↓␈α∞is␈α∞a␈α∞graphical␈α∂language␈α∞for␈α∞the␈α∞description␈α∞of␈α∂both␈α∞data␈α∞and␈α∞algorithms.␈α∂ We␈α∞will
␈↓explore␈α∞a␈α∞few␈α∞aspects␈α
of␈α∞AMBIT,␈α∞using␈α∞it␈α
only␈α∞to␈α∞describe␈α∞most␈α
of␈α∞the␈α∞basic␈α∞operation␈α∞of␈α
LISP.
␈↓AMBIT␈α
is␈α
a␈α
powerful␈α
graphical␈α
language␈α
suitable␈α
for␈α
describing␈α
complicated␈α
data␈α
structures␈α
and
␈↓their␈αmanipulation.␈αA␈αcrucial␈αproblem␈αof␈αnon-numerical␈αapplications␈αis␈αthe␈αstructuring␈αof␈αthe␈αdata.
␈↓Indeed␈α
it␈α
is␈α
frequently␈α
the␈α
case␈α
that␈α
the␈α
structuring␈α
of␈α
the␈α
data␈α
is␈α
the␈α
␈↓↓only␈↓␈α
problem;␈α
once␈αthe␈α
proper

________________
␈↓␈↓π 10␈↓␈αAn␈α
ambit␈αis␈αhalf␈α
aminal,␈αhalf␈α
hobitt.␈αAMBIT/G␈αalso␈α
is␈αan␈α
acronym␈αfor␈αAlgebraic␈α
Manipulation
␈↓By␈α
Identity␈α
Transformation/Graphical.
␈↓␈↓↓104  symbol tables␈↓ 56.9␈↓


␈↓representation␈α≠has␈α~been␈α≠established␈α~a␈α≠very␈α~simple␈α≠program␈α~to␈α≠manipulate␈α≠the␈α~complex
␈↓representation␈αwill␈α
suffice.␈αOften␈αwhat␈α
appears␈αto␈α
be␈αa␈αcomplicated␈α
algorithm␈αis,␈α
in␈αreality,␈αa␈α
simple
␈↓algorithm␈α∞operating␈α∞on␈α∞complex␈α∂data.␈α∞A␈α∞poor␈α∞representation␈α∂will␈α∞lead␈α∞to␈α∞an␈α∂inefficient␈α∞program.
␈↓Therefore␈α
in␈α
AMBIT,␈α
programming␈α
is␈α
attempted␈α
only␈α
after␈α
the␈α
data␈α
design␈α
has␈α
been␈α
completed.

␈↓Since␈α
the␈αinterrelationships␈α
between␈αdata␈α
structures␈αare␈α
inherently␈αmore␈α
static␈αthan␈α
the␈αexecuting␈α
of
␈↓an␈α∞algorithm,␈α∂it␈α∞is␈α∂also␈α∞beneficial␈α∂to␈α∞separate␈α∂complex␈α∞data␈α∂from␈α∞simple␈α∂program␈α∞␈↓π 11␈↓.␈α∂ Proofs␈α∞of
␈↓correctness␈α⊃of␈α⊃programs␈α⊃designed␈α∩in␈α⊃this␈α⊃way␈α⊃should␈α⊃also␈α∩be␈α⊃more␈α⊃easily␈α⊃obtained.␈α∩There␈α⊃are
␈↓difficulties␈α⊂in␈α⊂designing␈α⊂programs␈α∂in␈α⊂this␈α⊂manner;␈α⊂a␈α⊂significant␈α∂difficulty␈α⊂lies␈α⊂in␈α⊂the␈α⊂paucity␈α∂of
␈↓notation␈α∞which␈α∞we␈α∞have␈α∞available␈α∞for␈α∞describing␈α∞algorithms.␈α∞Current␈α∞notations␈α∞for␈α∞programming
␈↓languages␈α∀are␈α∀derived␈α∀from␈α∀linear␈α∀mathematical␈α∀notations.␈α∀These␈α∀notations␈α∀are␈α∀ill-suited␈α∪for
␈↓describing␈α∩data␈α⊃structure␈α∩representations.␈α⊃As␈α∩a␈α∩very␈α⊃simple␈α∩case,␈α⊃functions␈α∩in␈α∩mathematics␈α⊃are
␈↓single-valued,␈α
but␈α
often␈α
in␈α
programming␈α
we␈α
would␈αlike␈α
to␈α
return␈α
more␈α
than␈α
one␈α
value.␈αIn␈α
particular
␈↓we␈αwould␈αlike␈α
to␈αreturn␈αvalues␈α
which␈αcan␈αbe␈α
used␈αimmediately␈αas␈α
arguments␈αto␈αanother␈αfunction.␈α
A
␈↓generalized␈α∞composition␈α∞if␈α∞you␈α∞wish.␈α∂ Mathematical␈α∞notation␈α∞is␈α∞not␈α∞conducive␈α∞to␈α∂such␈α∞operations
␈↓even␈α
though␈α
the␈α
operations␈α
is␈α
quite␈α
natural␈α
and␈α
easily␈α
understood␈α
from␈α
the␈α
model␈α
of␈α
execution.

␈↓If␈α∞we␈α∞do␈α∞not␈α∂want␈α∞to␈α∞use␈α∞a␈α∞high␈α∂level␈α∞language␈α∞then␈α∞the␈α∞other␈α∂common␈α∞choice␈α∞is␈α∞to␈α∂encode␈α∞the
␈↓algorithm␈α∀in␈α∃machine␈α∀language.␈α∀The␈α∃objections␈α∀to␈α∃this␈α∀approach␈α∀are␈α∃clear:␈α∀the␈α∃program␈α∀is
␈↓incomprehensible␈α
to␈α
other␈α
people,␈α
and␈α
all␈α
too␈α
frequently␈α
it␈α
is␈α
poorly␈α
understood␈α
even␈α
by␈α
its␈α
creator.

␈↓When␈αwe␈αthink␈α
about␈αwriting␈αa␈α
complex␈αnon-numerical␈αprogram␈α
like␈αa␈αcompiler,␈α
a␈αproof-checker,
␈↓or␈αa␈αpiece␈αof␈αa␈αlarge␈αsystem,␈αwe␈αdraw␈αpictures␈αto␈αhelp␈αcrystalize␈αour␈αideas␈αand␈αto␈αstructure␈αthe␈αdata
␈↓efficiently.␈α When␈αfaced␈αwith␈αexplaining␈αa␈αcomplex␈αstructure-manipulating␈αprogram␈αto␈αsomeone␈αwe
␈↓draw␈αa␈αpicture.␈α
Clearly␈αthen,␈αa␈αgraphical␈α
notation␈αfor␈αprogramming␈α
is␈αworth␈αexploring.␈α AMBIT␈α
is
␈↓such␈α
a␈α
language.

␈↓First,␈α∂the␈α∂data␈α∞representation␈α∂in␈α∂AMBIT␈α∂is␈α∞a␈α∂generalization␈α∂of␈α∂the␈α∞␈↓↓box␈α∂notation␈↓␈α∂of␈α∂LISP.␈α∞ We
␈↓have␈αalready␈αseen␈αthat␈αit␈αis␈αoften␈αhelpful␈αto␈αdraw␈αpictures␈αto␈αunderstand␈αthe␈αdata␈αrepresentation␈αin
␈↓a␈α∞particular␈α∞problem.␈α∞ Instead␈α∞of␈α∞requiring␈α∞that␈α∞we␈α∞draw␈α∞all␈α∞nodes␈α∞in␈α∞a␈α∞tree␈α∞as␈α∞boxes,␈α∞we␈α
might
␈↓convey␈α⊃more␈α⊃of␈α⊃our␈α⊃intuition␈α⊃by␈α⊃drawing␈α⊃nodes␈α⊃of␈α⊃different␈α⊃shapes␈α⊃to␈α⊃represent␈α⊃nodes␈α⊃which
␈↓contain␈αdifferent␈α
kinds␈αof␈α
information.␈α We␈α
have␈αalready␈α
begun␈αto␈α
do␈αthis␈α
on␈αa␈α
small␈αscale.␈α
 Words
␈↓(nodes)␈α↔in␈α⊗Full␈α↔Word␈α⊗Space␈α↔are␈α⊗drawn:   ␈↓ε[~~~~~~]␈↓   ␈α↔whereas␈α⊗nodes␈α↔in␈α⊗free␈α↔space␈α⊗are
␈↓drawn:   ␈↓ε[~~~~]~~~~]␈↓   ␈α∂even␈α∂though␈α⊂as␈α∂far␈α∂as␈α∂most␈α⊂machines␈α∂are␈α∂concerned␈α∂both␈α⊂kinds␈α∂of
␈↓nodes␈α
map␈α
into␈α
the␈α
same␈α
kind␈α
of␈α
memory␈α
cell.

␈↓AMBIT/G␈αexploits␈αthis␈αgeneralization␈αof␈αshapes.␈α For␈αexample␈α␈↓π 12␈↓␈αin␈αa␈αtyped␈αlanguage␈αcontaining
␈↓types␈α
integer,boolean,␈α
structure,and␈α
real␈α
we␈α
represent␈α
these␈α
modes␈α
as:




________________
␈↓␈↓π 11␈↓␈α
Perlis:␈α
"suppress␈α
the␈α
constant␈α
and␈α
display␈α
the␈α
variable".

␈↓␈↓π 12␈↓␈α
These␈α
examples␈α
are␈α
taken␈α
from␈α
the␈α
formal␈α
definiton␈α
of␈α
BASEL.
␈↓␈↓↓6.9␈↓ 	rAMBIT/G     105␈↓








␈↓␈↓ ¬-␈↓↓Example of type shapes

␈↓We␈α
might␈α
represent␈α
the␈α
result␈α
of␈α
executing␈α
of␈α
␈↓αx␈α
←␈α
2␈↓,␈α
where␈α
␈↓αx␈↓␈α
is␈α
of␈α
type␈α
␈↓αint␈↓,␈α
as:











␈↓If␈α
the␈α
language␈α
also␈α
has␈α
type␈α
definition␈α
facilities,␈α
then␈α
we␈α
might␈α
represent␈α
mode␈α
␈↓αcomplex␈↓␈α
as:










␈↓␈↓ ¬←␈↓↓Mode of ␈↓αcomplex␈↓↓.










␈↓␈↓ ¬H␈↓αy ␈↓is␈↓α complex[1.0 ; 1.0]

␈↓This␈αis␈αinteresting␈αand␈αuseful␈α
notation␈αbut␈αin␈αitself␈αgives␈αus␈α
no␈αnew␈αpower.␈α The␈αinnovation␈αis␈α
that
␈↓␈↓↓106  symbol tables␈↓ 56.9␈↓


␈↓we␈α∞can␈α∞also␈α
describe␈α∞our␈α∞algorithms␈α∞as␈α
graphical␈α∞transformations␈α∞of␈α
the␈α∞data␈α∞graphs.␈α∞ The␈α
basic
␈↓statements␈α∂of␈α⊂the␈α∂language␈α∂are␈α⊂␈↓↓pattern-match-␈α∂and-replacement␈↓␈α∂rules.␈α⊂ That␈α∂is,␈α∂if␈α⊂a␈α∂designated
␈↓pattern␈α∞can␈α∞be␈α
found␈α∞in␈α∞the␈α∞current␈α
state␈α∞of␈α∞the␈α
data␈α∞graph,␈α∞then␈α∞replace␈α
it␈α∞with␈α∞a␈α∞new␈α
pattern.
␈↓The␈α
only␈α
kind␈α
of␈α
replacement␈α
allowed␈α
is␈α
the␈α
␈↓↓swinging␈↓␈α
of␈α
an␈α
arrow␈α
so␈α
that␈α
its␈α
head␈α
moves␈α
from␈α
one
␈↓node␈α∂to␈α⊂another.␈α∂ Where␈α⊂the␈α∂arrow␈α∂head␈α⊂strikes␈α∂a␈α⊂node␈α∂is␈α∂immaterial;␈α⊂the␈α∂origin␈α⊂of␈α∂the␈α⊂tail␈α∂␈↓↓is␈↓
␈↓important.␈α The␈αtails␈αof␈αthe␈αarrows␈αare␈αfixed␈α(as␈αare␈αthe␈αnumber␈αof␈αdata␈αnodes␈αand␈αarrows).␈α The
␈↓individual␈α⊃statements␈α⊃are␈α⊃combined␈α⊃into␈α⊃an␈α∩algorithm␈α⊃by␈α⊃success␈α⊃and␈α⊃failure␈α⊃conditions.␈α∩ If␈α⊃a
␈↓pattern-match-replacement␈α
is␈αsuccessful␈α
then␈αwe␈α
go␈αto␈α
one␈αAMBIT/G␈α
statement,␈αif␈α
the␈α
match␈αfails
␈↓then␈α
we␈α
go␈α
to␈α
another␈α
statement.

␈↓The␈α∩other␈α∩two␈α∩components␈α∩of␈α∩an␈α∩AMBIT/G␈α∩program␈α∩are␈α∩declaration␈α∩and␈α∪initialization.␈α∩ We
␈↓declare␈α⊃the␈α⊃shapes␈α⊃which␈α⊃will␈α⊃be␈α⊃used␈α⊃in␈α⊃the␈α⊃algorithm␈α⊃and␈α⊃declare␈α⊃the␈α⊃number␈α⊃and␈α⊂relative
␈↓positions␈α∞of␈α
the␈α∞arrows␈α∞which␈α
can␈α∞radiate␈α
from␈α∞each␈α∞type␈α
of␈α∞node.␈α
 The␈α∞initialization␈α∞(which␈α
we
␈↓usually␈α
suppress)␈α
sets␈α
the␈α
initial␈α
pattern␈α
of␈α
the␈α
data␈α
graph.␈α
 An␈α
example␈α
is␈α
long␈α
overdue:
























␈↓↓␈↓ ∧Kpicture of AMBIT/G algorithm for ␈↓αcar␈↓↓


␈↓This␈α
algorithm␈α∞is␈α
represents␈α∞the␈α
action␈α∞of␈α
the␈α∞␈↓αcar␈↓␈α
routine.␈α∞ Arrows␈α
which␈α∞are␈α
suppressed␈α∞are␈α
not
␈↓needed␈αfor␈αmatching.␈α The␈αsolid␈αlinks␈αrepresent␈αlinks␈α
which␈αmust␈αbe␈αfound␈αif␈αthe␈αrule␈αis␈αto␈α
succeed
␈↓and␈αthe␈α
dotted␈αlinks␈αrepresent␈α
links␈αwhich␈α
are␈αto␈αbe␈α
set␈α(as␈α
solid␈αlinks)␈αif␈α
the␈αmatch␈αsucceedes.␈α
 The
␈↓exit␈αlabeled␈αS␈αis␈αused␈αif␈αall␈αgoes␈αas␈αplanned.␈αThe␈αcircle␈αis␈αa␈αnotational␈αconvenience:␈αit␈αmatches␈αany
␈↓␈↓↓6.9␈↓ 	rAMBIT/G     107␈↓


␈↓shape.␈α∂ Thus␈α∂in␈α∂this␈α∂example,␈α∂␈↓αcar␈↓␈α∞would␈α∂succeed␈α∂as␈α∂long␈α∂as␈α∂AC1␈α∞points␈α∂to␈α∂a␈α∂cell␈α∂in␈α∂free␈α∞space;
␈↓otherwise␈α
we␈α
get␈α
an␈α
error.

␈↓Or␈α
here's␈α
a␈α
more␈α
complicated␈α
example:











␈↓In␈α
this␈αpiece␈α
of␈αprogram␈α
we␈αhave␈α
a␈αnew␈α
element:␈αnodes␈α
or␈α(solid)␈α
links␈αwhose␈α
perimeter␈α
is␈αcircled
␈↓are␈α
to␈α
be␈α
tested␈α
after␈α
the␈α
match␈α
has␈α
been␈α
made␈α
and␈α
before␈α
any␈α
specified␈α
modifications␈αare␈α
executed.
␈↓If␈α
the␈α
tests␈α
fail␈α
the␈α
the␈α
F␈α
exit␈α
is␈α
taken␈α
from␈α
the␈α
program.

␈↓The␈α
general␈α∞execution␈α
of␈α∞an␈α
AMBIT/G␈α∞block␈α
can␈α
be␈α∞described␈α
in␈α∞the␈α
usual␈α∞paradigm:␈α
selection,
␈↓testing,␈α
and␈α
construction.

␈↓First␈αthe␈αdata␈αis␈αselected.␈αA␈αmatch␈αof␈αthe␈αnodes␈αand␈αsolid␈αlinks␈αis␈αattempted.␈α If␈αthis␈αmatch␈αcannot
␈↓be␈α
made,␈α
an␈α
error␈α
has␈α
occurred.

␈↓Next,␈α
the␈α
testing␈α
of␈α
circled␈α
components␈α
is␈α
done.␈α
If␈α
a␈α
test␈α
fails,␈α
then␈α
the␈α
failure␈α
exit␈α
is␈α
taken.

␈↓Finally␈α⊂the␈α⊂constructions,␈α⊂designated␈α⊂by␈α⊂the␈α⊂dotted␈α⊂links,␈α⊂are␈α⊂performed␈α⊂and␈α⊂the␈α⊂success␈α⊂exit␈α∂is
␈↓taken.

␈↓****␈α
add␈α
technical␈α
here:␈α
data␈α
graph␈α
permanence,␈α
functionality,␈α
constranits␈α
and␈α
general␈α
b.s.***

␈↓On␈α∃following␈α∃pages␈α⊗are␈α∃AMBIT/G␈α∃algorithms␈α⊗for␈α∃␈↓αcdr␈↓,␈α∃␈↓αcons␈↓,␈α⊗␈↓αeq␈↓,␈α∃and␈α∃␈↓αatom␈↓.␈α⊗ The␈α∃following
␈↓conventions␈α
pertain␈α
to␈α
these␈α
implementations:

␈↓␈↓↓1.␈↓␈α⊃We␈α⊃assume␈α⊃AC1,␈α⊃AC2,␈α∩...,ACn␈α⊃are␈α⊃pointers␈α⊃to␈α⊃the␈α∩n␈α⊃arguments␈α⊃of␈α⊃a␈α⊃function␈α∩(requiring␈α⊃n
␈↓arguments).

␈↓␈↓↓2.␈↓␈αWe␈αassume␈αthat␈αevery␈αfunction,␈αon␈αcompletion␈αof␈αits␈αaction,␈αwill␈αset␈αAC1␈αto␈αpoint␈αto␈αthe␈αvalue␈αof
␈↓that␈α
function.

␈↓There␈α∂are␈α∞three␈α∂basic␈α∞shapes:␈α∂  ␈↓εα~~~~]␈↓,  ␈↓ε[~~~]~~~]␈↓  ␈α∞and␈α∂  ␈↓ε[~~~~~~]␈↓  ␈α∂corresponding␈α∞to
␈↓atom␈α
headers,␈α
free␈α
space␈α
and␈α
full␈α
word␈α
space␈α
objects.␈α
 Pointers␈α
will␈α
be␈α
given␈α
a␈α
separate␈α
shape:   ␈↓εA  ␈↓;
␈↓and␈αwhen␈αwe␈αdescribe␈αthe␈αgarbage␈αcollector␈αwe␈αwill␈αneed␈αa␈α`marked-unmarked'␈αshape:␈α  ␈↓εG␈↓  ␈α.␈αOne
␈↓final␈α⊃point:␈α⊃the␈α⊃`down␈α⊃arrow'␈α⊃on␈α⊃  ␈↓ε[~~~]~~~]␈↓  ␈α⊃and␈α⊃  ␈↓ε[~~~~~~~]␈↓  ,␈α⊃is␈α⊃usually␈α⊃implicit␈α⊂in
␈↓implementations.␈α
What␈α
does␈α
it␈α
represent?
␈↓␈↓↓108  symbol tables␈↓ 56.9␈↓


␈↓******picture␈α
of␈α
shapes****






























␈↓↓␈↓ ¬YPicture of Shapes

␈↓***review***

␈↓Graphical␈αlanguages␈αare␈αa␈αvery␈αnatural␈αand␈αpowerful␈αmeans␈αof␈αdescribing␈αprocesses.␈α As␈αexamples,
␈↓the␈αnext␈αsections␈αwill␈αgive␈αtwo␈αLISP␈αgarbage␈αcollectors,␈αone␈αvery␈αnatural,␈αone␈αmore␈αcomplicated␈α
but
␈↓easily␈αunderstood;␈αthen␈αa␈αAMBIT/G␈αdescription␈αof␈αparsing␈αtechniques␈αdue␈αto␈αT.␈αCheatham,␈αcalled
␈↓syntactic␈α⊂dominoes;␈α⊂and␈α⊂finally␈α⊂an␈α⊂AMBIT/G␈α⊂description␈α⊂of␈α⊂the␈α⊂operators␈α⊂for␈α⊂the␈α⊂AI␈α⊂chestnut,
␈↓called␈α
the␈α
Monkey␈α
and␈α
Banannas␈α
problem.
␈↓␈↓↓6.10␈↓ λ0garbage collection again     109␈↓


␈↓␈↓ ¬
␈↓↓6.10  garbage collection again␈↓


␈↓Notes:␈α
The␈α
right-side␈α
arrows␈α
on␈α
  ␈↓εα~~~]␈↓  ␈α
and␈α
  ␈↓ε[~~~]~~~]␈↓  ␈α
are␈α
used␈α
by␈α
the␈αgarbage␈α
collector.
␈↓First␈α⊃we␈α⊃sweep␈α⊃from␈α⊃  ␈↓ε[~~~~~~]␈↓βTOP␈↓ ␈α⊂to  ␈↓ε[~~~~~~~~~]␈↓βBOTTOM␈↓ ,  ␈α⊃setting␈α⊃the␈α⊃side␈α⊃arrows␈α⊂to
␈↓␈↓εG␈↓βNA␈↓ ;

␈↓when␈α
we␈α
mark,␈α
we␈α∞reset␈α
those␈α
nodes␈α
which␈α∞are␈α
reachable␈α
to␈α
point␈α∞to␈α
␈↓εG␈↓βA␈↓.␈α
 Then␈α
the␈α∞final␈α
sweep
␈↓phase␈αwill␈αcollect␈αall␈αthose␈αnodes␈αstill␈αmarked,␈α␈↓εG␈↓βNA␈↓  ␈αinto␈αa␈αnew␈αfree␈αspace␈αlist,␈αpointed␈αto␈αby␈αFSP␈α.
␈↓␈↓εα[~~~~]␈↓β<name>␈↓  ␈α
is␈α
the␈α
atom␈α
header;␈α
<name>␈α
need␈α
not␈α
be␈α
present.

␈↓␈↓εA␈↓βMKR␈↓  ␈α
is␈α
a␈α
pointer␈α
used␈α
during␈α
the␈α
garbage␈α
collection.

␈↓␈↓εA␈↓βP␈↓  ␈α⊂is␈α∂a␈α⊂pointer␈α∂used␈α⊂to␈α∂save␈α⊂the␈α⊂cdr␈α∂parts␈α⊂of␈α∂lists␈α⊂during␈α∂the␈α⊂marking␈α∂phase␈α⊂of␈α⊂the␈α∂garbage
␈↓collector.

␈↓␈↓ε[~~~~]␈↓βTOP␈↓,  ␈α
␈↓ε[~~~~~~]␈↓βBOTTOM␈↓  ␈α
are␈α
used␈α
to␈α
delimit␈α
the␈α
boundaries␈α
of␈α
free␈α
space.

␈↓The␈αgarbage␈αcollector␈αhas␈αbeen␈αslightly␈αsimplified.␈α We␈αshould␈αmark␈αmore␈αthan␈αjust␈αOBLIST␈α(the
␈↓symbol␈α⊂table);␈α⊂for␈α⊂example,␈α⊂what␈α∂␈↓εA␈↓βAC1␈↓␈α⊂and␈α⊂␈↓εA␈↓βAC2␈↓␈α⊂point␈α⊂to␈α∂should␈α⊂be␈α⊂marked.␈α⊂ There␈α⊂are␈α∂other
␈↓intermediate␈α
results␈α
which␈α
must␈α
be␈α
preserved;␈α
these␈α
will␈α
become␈α
apparent␈α
as␈α
we␈α
proceed.

␈↓We␈α∂must␈α∞also␈α∂be␈α∞careful␈α∂about␈α∞the␈α∂order␈α∞in␈α∂which␈α∞the␈α∂dotted␈α∞lines␈α∂are␈α∞`executed';␈α∂often␈α∂we␈α∞will
␈↓number␈α
the␈α
arrows␈α
to␈α
indicate␈α
the␈α
order␈α
of␈α
execution.

␈↓Following␈αthis␈αintroduction␈αis␈αthe␈αmain␈αstructure␈αof␈αthe␈αfirst␈αLISP␈αgarbage␈αcollector.␈α The␈αheart␈αof
␈↓the␈α
collector␈α
is␈αthe␈α
marking␈α
algorithm.␈α
 Some␈αcare␈α
need␈α
be␈α
excercised␈αhere.␈α
Since␈α
we␈α
are␈αmarking
␈↓binary␈αlist␈αstructure␈αin␈αa␈αsequential␈αmanner,␈αwe␈αneed␈αto␈αmake␈αa␈αdecision␈αas␈αto␈αwhether␈αto␈αmark␈αthe
␈↓␈↓αcar␈↓-part␈α
first␈α
or␈α
mark␈α
the␈α
␈↓αcdr␈↓-part␈α
first.␈α
 Actually␈α
the␈α
order␈α
in␈α
which␈α
we␈α
do␈α
these␈α
operations␈α
isn't
␈↓important.␈α⊂ What␈α⊃␈↓↓is␈↓␈α⊂important␈α⊃is␈α⊂that␈α⊃while␈α⊂we␈α⊂are␈α⊃marking␈α⊂the␈α⊃first-chosen␈α⊂branch,␈α⊃we␈α⊂must
␈↓remember␈αwhere␈αthe␈αother␈αbranch␈αis␈αso␈αthat␈αwe␈αcan␈αsubsequently␈αmark␈αit.␈α Also␈αsince␈αthis␈αmarking
␈↓process␈α∩is␈α∩obviously␈α∩recursive␈α∩--␈α∩branches␈α∩may␈α⊃also␈α∩have␈α∩branches--␈α∩we␈α∩must␈α∩be␈α∩prepared␈α⊃to
␈↓remember␈αan␈α
arbitrary␈αnumber␈α
of␈α`other␈α
branches'.␈α The␈α
pointer,␈αP,␈α
is␈αused␈α
to␈αhelp␈αremember␈α
these
␈↓pending␈α
branches.

␈↓As␈α
you␈α
chase␈α
the␈α
arrows␈α
in␈α
the␈α
AMBIT/G␈α
marking␈α
algorithm,␈α
notice␈α
that:

␈↓␈↓↓1.␈↓␈α
We␈α
always␈α
mark␈α
the␈α
␈↓αcar␈↓-branch␈α
first.␈α
This␈α
is␈α
usually␈α
called␈α
pre-order␈α
traversal.

␈↓␈↓↓2.␈↓␈αAs␈αwe␈αprepare␈αto␈α
examine␈αthe␈α␈↓αcar␈↓-part␈αof␈αa␈α
tree␈αwe␈αsave␈αa␈αpointer␈α
to␈αthat␈αnode␈αof␈αthe␈αtree,␈α
using
␈↓P.

␈↓␈↓α3.␈↓␈α
After␈α
we␈α
have␈α
marked␈α
the␈α
␈↓αcar␈↓-part,␈α
we␈α
use␈α
the␈α
information␈α
saved␈α
by␈α
P␈α
to␈α
traverse␈α
to␈α
␈↓αcdr␈↓-part.

␈↓P␈αis␈αpointing␈αto␈αa␈αsequence␈αof␈αcells␈αlinked␈αtogether␈αby␈αtheir␈α`down␈αarrows'.␈α When␈αwe␈αgo␈αdown␈αthe
␈↓␈↓↓110  symbol tables␈↓ )6.10␈↓


␈↓␈↓αcar␈↓-part,␈αwe␈αsave␈αthe␈αparent-node␈αin␈αthe␈αcell␈αcurrently␈αbeing␈αpointed␈αto␈αby␈αP.␈α Then␈αwe␈αset␈αP␈αto␈αit's
␈↓`down␈α
arrow'␈α
successor:










␈↓␈↓ ε/␈↓↓Push


␈↓As␈αlong␈αas␈αthe␈α␈↓αcar␈↓-part␈αof␈αthe␈αstructure␈α
we␈αare␈αmarking␈αpoints␈αinto␈αfree␈αspace,␈αwe␈αwill␈α
continue␈αto
␈↓update␈αP.␈α If␈αyou␈αlook␈αback␈αthrough␈αthe␈αelements␈αwhcih␈αwe␈αhave␈αadded␈αto␈αP␈αyou␈αwill␈αsee␈αa␈α
history
␈↓of␈α∞those␈α∞nodes␈α∞whose␈α∂␈↓αcar␈↓-parts␈α∞have␈α∞been␈α∞marked,␈α∞but␈α∂whose␈α∞␈↓αcdr␈↓-parts␈α∞are␈α∞still␈α∞to␈α∂be␈α∞examined.
␈↓When␈αwe␈α
finally␈αterminate␈α
the␈α␈↓αcar␈↓-marking␈α
we␈αwill␈α
pick␈αoff␈α
the␈αlast␈α
node␈αin␈α
P,␈αdecrement␈α
P,␈αand
␈↓then␈α
traverse␈α
that␈α
substructure:










␈↓␈↓ ε5␈↓↓Pop


␈↓Thus,␈α
P␈α
and␈α
its␈α
associated␈α
cells␈α
are␈α
behaving␈α
as␈α
a␈α
stack.

␈↓Recall␈αthat␈α
we␈αare␈αstoring␈α
information␈αas␈αlist␈α
structure␈αand␈αthus␈α
have␈αintersecting␈α
branches.␈α Now
␈↓since␈α
we␈α
do␈α
have␈α
intersections,␈αthe␈α
marking␈α
process␈α
can␈α
be␈α
a␈αlittle␈α
clever.␈α
 As␈α
soon␈α
as␈α
the␈αmarker
␈↓comes␈α
across␈α
a␈α
previously␈α∞marked␈α
cell␈α
we␈α
know␈α
that␈α∞everything␈α
below␈α
that␈α
point␈α
in␈α∞the␈α
structure
␈↓has␈α
already␈α
been␈α
marked.␈α
 We␈α
can␈α
then␈α
terminate␈α
the␈α
marker␈α
on␈α
that␈α
branch.

␈↓Here␈αthen,␈αis␈αthe␈αsimple␈αAMBIT/G␈αgarbage␈αcollector␈αfor␈αLISP␈αfollowed␈αby␈αa␈αpartial␈αdescription␈αin
␈↓LISP.
␈↓␈↓↓6.10␈↓ λ0garbage collection again     111␈↓


␈↓␈↓ ∧→␈↓↓A simple LISP garbage collector in AMBIT/G␈↓
␈↓␈↓↓6.10␈↓ λ0garbage collection again     113␈↓


␈↓␈↓ ∧¬␈↓↓A simple LISP garbage collector written in LISP␈↓


␈↓We␈αwill␈αnow␈αwrite␈αa␈αgarbage␈αcollector␈αin␈αLISP␈αto␈αmark␈αand␈αsweep␈αnodes␈αin␈αFS␈αand␈αFWS.␈α It␈αwill
␈↓have␈α
three␈α
main␈α
functions:

␈↓␈↓αinitialize[x;y]␈↓␈α
to␈α
initialize␈α
the␈α
marking␈α
device␈α
for␈α
each␈α
cell␈α
in␈α
the␈α
space␈α
from␈α
␈↓αx␈↓␈α
to␈α
␈↓αy␈↓.

␈↓␈↓αmark[l]␈↓␈α
to␈α
do␈α
the␈α
actual␈α
marking␈α
of␈α
a␈α
list␈α
␈↓αl␈↓.

␈↓␈↓αsweep[x;y]␈↓␈α
to␈α
collect␈α
all␈α
inaccessible␈α
cells␈α
in␈α
the␈α
space␈α
delimited␈α
by␈α
␈↓αx␈↓␈α
and␈α
␈↓αy␈↓.

␈↓␈↓αinitialize␈↓␈α∞will␈α
be␈α∞called␈α
twice;␈α∞once␈α
for␈α∞FS␈α
and␈α∞once␈α
for␈α∞FWS.␈α
 ␈↓αmark␈↓␈α∞will␈α
be␈α∞called␈α
for␈α∞each␈α
base
␈↓register␈α
which␈α
points␈α
to␈α
active␈α
(binary)␈α
list␈α
structure.␈α The␈α
basic␈α
structure␈α
of␈α
the␈α
marker␈α
is:␈α
if␈αthe
␈↓word␈αis␈αin␈αFWS␈αmark␈αit␈αand␈αleave;␈α
if␈αthe␈αword␈αhas␈αalready␈αbeen␈αmarked␈αsimply␈α
leave.␈αOtherwise
␈↓the␈αword␈αis␈αin␈αFS␈αand␈αthus␈αhas␈αa␈α␈↓αcar␈↓␈αand␈αa␈α␈↓αcdr␈↓;␈αmark␈αthe␈αword;␈αmark␈αthe␈α␈↓αcar␈↓;␈αmark␈αthe␈α
␈↓αcdr␈↓.␈αThe
␈↓marker␈α⊃is␈α⊂thus␈α⊃recursive;␈α⊃all␈α⊂of␈α⊃the␈α⊂stack␈α⊃manipulation␈α⊃done␈α⊂by␈α⊃the␈α⊂AMBIT␈α⊃program␈α⊃will␈α⊂be
␈↓hidden␈α
by␈α
the␈α
recursion.

␈↓␈↓αsweep␈↓␈α
will␈α
be␈α
called␈α
twice;␈α
once␈α
to␈α
generate␈α
a␈α
new␈α
free␈α
space␈α
list␈α
and␈α
once␈α
to␈α
generate␈α
a␈α∞new␈α
full
␈↓word␈α⊃space␈α⊃list.␈α⊃Elements␈α⊃of␈α⊃these␈α⊃free␈α⊃lists␈α⊃will␈α⊃be␈α⊃chained␈α⊃together␈α⊃by␈α⊃their␈α⊃␈↓αcdr␈↓␈α⊃parts.␈α⊃ The
␈↓initialization␈α⊂and␈α⊂sweep␈α∂phases␈α⊂of␈α⊂this␈α⊂garbage␈α∂collector␈α⊂are␈α⊂very␈α∂similar;␈α⊂both␈α⊂phases␈α⊂must␈α∂be
␈↓assured␈α
of␈α
reaching␈α
every␈α
node␈α
in␈α
the␈α
space.␈α
We␈α
used␈α
the␈α
␈↓αdown␈↓-arrows␈α
for␈α
this.

␈↓These␈α
main␈α
functions␈α
use␈α
several␈α
other␈α
functions␈α
and␈α
predicates:

␈↓␈↓αfwswrdp[x]␈↓␈αis␈αtrue␈αjust␈α
in␈αthe␈αcase␈αthat␈α
␈↓αx␈↓␈αis␈αa␈αword␈α
in␈αFWS.␈αThis␈αis␈α
used␈αas␈αone␈αof␈αthe␈α
termination
␈↓␈↓ αhconditions␈α
of␈α
␈↓αmark␈↓.

␈↓␈↓αmakeA[x]␈↓␈α
marks␈α
word␈α
␈↓αx␈↓␈α
as␈α
accessible.

␈↓␈↓αmakeNA[x]␈↓␈α
marks␈α
word␈α
␈↓αx␈↓␈α
as␈α
not␈α
accessible.

␈↓␈↓αNAp[x]␈↓␈α
is␈α
true␈α
if␈α
word␈α
␈↓αx␈↓␈α
is␈α
not␈α
accessible.

␈↓␈↓αdown[x]␈↓␈α
follows␈α
the␈α
"down"␈α
link␈α
of␈α
the␈α
node␈α
␈↓αx␈↓.

␈↓␈↓αconca[x;y]␈↓␈αattaches␈αnode␈α
␈↓αx␈↓␈αthe␈αthe␈α
front␈αof␈αthe␈α
list␈α␈↓αy␈↓.␈α The␈α
value␈αreturned␈αis␈α
the␈αnew␈αlist.␈α
 ␈↓αconca␈↓␈αis
␈↓␈↓ αheasily␈α
described␈α
in␈α
AMBIT.␈α
Can␈α
you␈α
write␈α
␈↓αconca␈↓␈α
as␈α
a␈α
LISP␈α
function?
␈↓␈↓↓114  symbol tables␈↓ )6.10␈↓









␈↓↓␈↓ ¬"AMBIT diagam for ␈↓αconca␈↓↓.

␈↓αinitialize <= λ[[x;y]
␈↓α␈↓ βXprog[[]
␈↓α␈↓ βX a␈↓ βxmakeNA[x];
␈↓α␈↓ βX␈↓ βxx ← down[x];
␈↓α␈↓ βX␈↓ βx[eq[x;y] → return[T]]
␈↓α␈↓ βX␈↓ βxgo [a]]]


␈↓αmark <=    λ[[l] [␈↓ βXnot[NAp[l]] → T;
␈↓α␈↓ βXfwswrdp[l] → makeA[l];
␈↓α␈↓ βXT → makeA[l];mark[car[l]];mark[cdr[l]] ]]


␈↓αsweep <= λ[[x;y]
␈↓α␈↓ βXprog[[z]
␈↓α␈↓ βX a␈↓ βx[NAp[x] → z← conca[ x;z]];
␈↓α␈↓ βX␈↓ βxx ← down[x];
␈↓α␈↓ βX␈↓ βx[eq[x;y] → return [z]];
␈↓α␈↓ βX␈↓ βxgo[a]]]




␈↓␈↓ ∧2␈↓↓A link-bending garbage collector for LISP␈↓


␈↓The␈α
description␈αof␈α
this␈αvery␈α
simple␈αcollector␈α
is␈αeasily␈α
understood␈αeither␈α
as␈αa␈α
LISP␈αfunction␈α
or␈αas␈α
an
␈↓AMBIT␈α
program.␈α
The␈α
collector␈α
we␈α
are␈α∞about␈α
to␈α
describe␈α
is␈α
more␈α
naturally␈α∞described␈α
graphically.
␈↓First␈α
notice␈α
that␈α
the␈αAMBIT␈α
program␈α
uses␈α
an␈α
explicit␈αstack␈α
to␈α
store␈α
traversing␈αinformation␈α
whereas
␈↓in␈α
the␈α
LISP␈α
description␈α
the␈α
use␈α
of␈α
a␈α
stack␈α
is␈α
implicit␈α
in␈α
the␈α
recursion␈α
of␈α
␈↓αmark␈↓.

␈↓The␈αuse␈αof␈αa␈αstack␈αis␈αone␈αof␈αthe␈αdifficulties␈αassociated␈αwith␈αthis␈αsimple␈αcollector.␈αGarbage␈αcollection
␈↓is␈αinvoked␈αwhen␈αavailable␈αspace␈αhas␈αbecome␈αexhausted,␈αbut␈αhere␈αwe␈αare,␈αasking␈αfor␈α␈↓↓more␈↓␈αspace␈αto
␈↓use␈α
for␈α∞stacking.␈α
 The␈α∞usual␈α
solution␈α∞is␈α
to␈α∞allocate␈α
a␈α∞separate␈α
area␈α∞for␈α
stack␈α∞storage.␈α
This␈α∞has␈α
its
␈↓drawbacks.␈αIf␈αwe␈αdon't␈αallocate␈αenough␈αstack␈αspace,␈α
i.e.,␈αif␈αthe␈αdepth␈αof␈αa␈αpiece␈αof␈αstructure␈α
becomes
␈↓too␈α∞great,␈α∞then␈α∞the␈α∞marker␈α
will␈α∞fail.␈α∞ Notice␈α∞to␈α∞that␈α∞the␈α
amount␈α∞of␈α∞stack␈α∞space␈α∞can␈α∞become␈α
large;
␈↓proportional␈α
to␈α
the␈α
length␈α
of␈α
a␈α
list.
␈↓␈↓↓6.10␈↓ λ0garbage collection again     115␈↓











␈↓Another␈α∂means␈α∂of␈α∂dispensing␈α∂with␈α∞a␈α∂stack␈α∂when␈α∂traversing␈α∂a␈α∞tree␈α∂is␈α∂to␈α∂use␈α∂a␈α∂more␈α∞complicated
␈↓representation␈α⊃for␈α⊃each␈α⊃node.␈α⊃Notice␈α⊃that␈α⊃if␈α⊃we␈α⊃examined␈α⊃"snapshots"␈α⊃of␈α⊃the␈α⊃execution␈α⊃of␈α⊃the
␈↓traversal␈αof␈αa␈αtree␈αas␈αlong␈αas␈αwe␈αtraversed␈αthe␈αtree␈αeach␈αtime␈αin␈αthe␈αsame␈αorder,␈αthe␈αcontents␈αof␈αthe
␈↓stack␈α
would␈α
be␈α
identical␈α
␈↓π 13␈↓.␈α
Instead␈α
of␈α
replicating␈α
this␈α
portion␈α
of␈α
a␈α
stack␈α
on␈α
each␈α
traversal␈αwe␈α
could
␈↓save␈α⊂the␈α⊂stack␈α⊂information␈α⊂with␈α⊂the␈α⊂nodes␈α⊂in␈α⊂the␈α⊂tree.␈α⊂ This␈α⊂technique␈α⊂is␈α⊂called␈α⊃threading␈α⊂␈↓π 14␈↓.
␈↓Threading␈α∩complicated␈α⊃the␈α∩representation␈α⊃and␈α∩causes␈α∩some␈α⊃difficulties␈α∩when␈α⊃we␈α∩wish␈α∩to␈α⊃store
␈↓list-structure␈α
rather␈α∞than␈α
trees.␈α∞However␈α
the␈α∞idea␈α
of␈α
dispensing␈α∞with␈α
the␈α∞stack␈α
␈↓↓is␈↓␈α∞worthwhile.␈α
We
␈↓can␈α
strike␈α
a␈α
comproimise.␈αInstead␈α
of␈α
permanently␈α
storing␈α
the␈αthreads␈α
in␈α
the␈α
structure␈α
we␈αcan␈α
modify
␈↓the␈α∂structure␈α∂as␈α∞we␈α∂traverse␈α∂it,␈α∂use␈α∞the␈α∂threads␈α∂to␈α∞mark,␈α∂and␈α∂then␈α∂to␈α∞restore␈α∂the␈α∂structure␈α∂to␈α∞its
␈↓original␈α⊃topology.␈α⊃ Certainly␈α⊃the␈α⊂marker␈α⊃will␈α⊃be␈α⊃more␈α⊂complicated,␈α⊃but␈α⊃the␈α⊃complication␈α⊃is␈α⊂not
␈↓insurmountable.␈α
In␈α
fact,␈α
it␈α
is␈α
even␈α
reasonably␈α
straightforward␈α
to␈α
prove␈α
the␈α
correctness␈α
of␈α
such␈αan
␈↓algorithm␈α
␈↓π 15␈↓.

␈↓Finally,␈α
here␈α
is␈α
such␈α
a␈α
"link-bending"␈α
marker␈α
written␈α
in␈α
AMBIT/G:

















________________
␈↓␈↓π 13␈↓␈α
except␈α
for␈α
perhaps␈α
the␈α
actual␈α
machine␈α
locations␈α
used␈α
in␈α
the␈α
stack

␈↓␈↓π 14␈↓␈α
See␈α
Knuth's␈α
Kompendium

␈↓␈↓π 15␈↓␈α
W.␈α
deRoever,␈α
private␈α
communication.
␈↓␈↓↓116  symbol tables␈↓ )6.10␈↓


␈↓To␈α∂reinforce␈α⊂ideas,␈α∂here␈α∂is␈α⊂a␈α∂simple␈α⊂data␈α∂graph␈α∂and␈α⊂a␈α∂sketch␈α⊂of␈α∂the␈α∂modifications␈α⊂to␈α∂it␈α⊂as␈α∂the
␈↓marker␈α
operates:
␈↓␈↓↓6.11␈↓ λrSyntactic dominoes     117␈↓


␈↓␈↓ ¬,␈↓↓6.11  Syntactic dominoes␈↓


␈↓The␈αmost␈αimportant␈αparts␈αof␈αour␈αdiscussions␈α
involve␈αdata␈αstructures,␈αi.e.,␈αsexprs␈αalready␈αin␈α
memory.
␈↓An␈α∞important,␈α∞but␈α∞peripheral␈α∞problem␈α∞is␈α∞the␈α∞input␈α∞such␈α∞text,␈α∞taking␈α∞the␈α∞linear␈α∂representation␈α∞of
␈↓sexpressions␈αinto␈αthe␈αbinary␈αlist-structure␈α
representation.␈αWe␈αhave␈αmentioned,␈αwhile␈αdiscussing␈α
␈↓αread␈↓
␈↓and␈α∂friends,␈α∞that␈α∂such␈α∞a␈α∂program␈α∞is␈α∂called␈α∂a␈α∞parser.␈α∂The␈α∞general␈α∂techniques␈α∞which␈α∂are␈α∂used␈α∞by
␈↓parsers␈α∪are␈α∩easily␈α∪described␈α∩using␈α∪a␈α∩game␈α∪called␈α∩syntactic␈α∪dominoes.␈α∩ This␈α∪clever␈α∪device␈α∩was
␈↓invented␈α
by␈α
T.␈α
Cheatham␈α
and␈α
is␈α
described␈α
in␈α
AMBIT/G.



␈↓␈↓ ¬,␈↓↓6.12  The Contour Model␈↓


␈↓Certain␈α∩phases␈α∩of␈α∩the␈α∪execution␈α∩of␈α∩LISP␈α∩programs,␈α∪in␈α∩particular␈α∩function␈α∩calls␈α∪and␈α∩variable
␈↓bindings␈α
are␈α
better␈α
described␈α
in␈α
another␈α
graphical␈α
language,␈α
the␈α
Contour␈α
Model.

␈↓This␈αmodel,␈αthough␈αwell␈αknown␈αintuitively␈αby␈αany␈αtranslator␈αwriter,␈αwas␈αrecently␈αmade␈αexplicit␈αand
␈↓embellished␈αby␈αJ.␈α
Johnston.␈αIts␈αability␈α
to␈αclearly␈αshow␈α
the␈αbinding␈αmechanisms␈α
in␈αblock-structured
␈↓languages␈α
is␈α
exceptional.

␈↓Note␈αfirst␈αthat␈α
this␈αmodel␈αis␈αa␈α
different␈αtype␈αthan␈αAMBIT/G.␈α
The␈αContour␈αModel␈αis␈α
essentially␈αa
␈↓trace␈α⊂of␈α∂the␈α⊂execution␈α⊂of␈α∂a␈α⊂specific␈α⊂program,␈α∂whereas␈α⊂AMBIT/G␈α⊂is␈α∂a␈α⊂reasonable␈α⊂candidate␈α∂for
␈↓describing␈α
the␈αsemantics␈α
of␈α
a␈αcomplete␈α
language.␈α
Indeed,␈αAMBIT/G␈α
has␈α
been␈αused␈α
to␈αdocument␈α
the
␈↓extensible␈α
language,␈α
BASEL,␈α
and␈α
LISP.

␈↓The␈α
simplest␈α
way␈α
to␈α
introduce␈α
the␈α
Contour␈α
Model␈α
is␈α
by␈α
example:
␈↓␈↓↓6.13␈↓ λ Macros and special forms     119␈↓


␈↓␈↓ ¬α␈↓↓6.13  Macros and special forms␈↓


␈↓Most␈αof␈αthe␈αdiscussion␈αto␈αthis␈αpoint␈αhas␈α
dealt␈αwith␈αgetting␈αa␈αnew␈αefficient␈αsymbol␈αtable␈α
which␈αcan
␈↓fulfill␈α∞our␈α∞requirements␈α∞of␈α∞permanence␈α∞and␈α∞multiplicity␈α∞of␈α∞properties.␈α∞Little␈α∞has␈α∞been␈α∂said␈α∞about
␈↓user␈α∞controlled␈α
function␈α∞calling,␈α
which␈α∞we␈α
called␈α∞the␈α
desire␈α∞for␈α
generality.␈α∞Before␈α∞introducing␈α
the
␈↓definition␈α
of␈α
the␈α
new␈α
evaluator␈α
we␈α
shall␈α
deal␈α
with␈α
that␈α
aspect.

␈↓Recall␈α⊂our␈α⊂discussion␈α⊂of␈α⊂special␈α⊂forms␈α⊂in␈α⊃Section␈α⊂4.13␈α⊂.␈α⊂Special␈α⊂forms␈α⊂have␈α⊂been␈α⊂used␈α⊃for␈α⊂two
␈↓purposes:␈αto␈αcontrol␈αthe␈αevaluation␈αof␈αarguments␈α(conditional␈αexpressions,␈αquoted␈α
expressions,␈α␈↓αand,
␈↓αor␈↓,␈αetc.),␈αand␈αto␈αcreate␈αthe␈αeffect␈αof␈αfunctions␈αwith␈αan␈αindefinite␈αnumber␈αof␈αarguments␈α(␈↓αlist,␈αappend,
␈↓αplus,␈α...␈↓)␈αFrequently␈αthis␈αsecond␈αapplication␈αof␈αspecial␈αforms␈αcan␈αbe␈αimproved␈αupon,␈αparticularly␈αin
␈↓the␈α∩presence␈α∩of␈α∩a␈α∩compiler.␈α∩ Describing␈α∪such␈α∩functions␈α∩as␈α∩special␈α∩forms␈α∩is␈α∩sufficient,␈α∪but␈α∩not
␈↓efficient,␈αsince␈αthe␈αbody␈αof␈αthe␈αdefinition␈αmust␈αcontain␈αexplicit␈αcalls␈αon␈α␈↓αeval␈↓.␈αEven␈αthough␈αwe␈αwish
␈↓to␈αdefine␈αsome␈α
functions␈αas␈αif␈α
they␈αhad␈αan␈αarbitrary␈α
number␈αof␈αarguments,␈α
when␈αwe␈α␈↓¬use␈↓␈α
(i.e.␈αcall)
␈↓the␈αfunction,␈αthe␈αnumber␈αof␈αarguments␈αto␈αbe␈αapplied␈α␈↓¬is␈↓␈αknown.␈αIn␈αparticular,␈αwhen␈αthe␈αcompiler␈αis
␈↓examining␈αthe␈αprogram,␈α
the␈αnumber␈αof␈α
arguments␈αis␈αknown.␈αHopefully␈α
the␈αcompiler␈αcan␈α
be␈αmade
␈↓smart␈α
enough␈α
to␈α
compile␈α
better␈α
code␈α
than␈α
calls␈α
on␈α
␈↓αeval␈↓.␈α
 That␈α
hope␈α
is␈α
well-founded.

␈↓Assume,␈α
for␈α
example,␈α
we␈α
wish␈α
to␈α
define␈α
␈↓αplus␈↓␈α
as␈α
a␈α
function␈α
with␈α
an␈α
indefinite␈α
number␈α
of␈α
arguments:
␈↓α␈↓ ¬zplus[4;5] = 9
␈↓α␈↓ ¬gplus[4;5;6] = 15
␈↓α␈↓ ¬Cplus[4;add1[2];4] = 11

␈↓That␈α
is␈α
␈↓αplus␈↓␈α
to␈αhave␈α
the␈α
properties␈α
of␈α
a␈αfunction:␈α
its␈α
arguments␈α
are␈αto␈α
be␈α
evaluated␈α
(from␈α
left␈αto
␈↓right);␈αbut␈αit␈αcan␈αtake␈αan␈αarbitrary␈αnumber.␈α What␈αis␈αneeded␈αhere␈αseems␈αclear:␈αdefine␈α␈↓αplus␈↓␈αin␈αterms
␈↓of␈α
a␈α
␈↓↓binary␈↓␈α
addition␈α
function,␈α
␈↓α*plus␈↓.
␈↓α␈↓ ¬&plus[4;5] = *plus[4;5] = 9
␈↓α␈↓ ∧Xplus[4;5;6] = *plus[4;*plus[5;6]] = 15
␈↓α␈↓ ∧∞plus[4;add1[2];4] = *plus[4;*plus[add1[2];4]] = 11

␈↓That␈α∞is,␈α∞we␈α∞␈↓↓expand␈↓␈α∞calls␈α∞on␈α∞␈↓αplus␈↓␈α∞into␈α∞a␈α∂composition␈α∞of␈α∞calls␈α∞on␈α∞␈↓α*plus␈↓.␈α∞ ␈↓αplus␈↓␈α∞is␈α∞being␈α∞used␈α∂as␈α∞a
␈↓␈↓↓macro␈↓␈α
and␈α
the␈α
expansion␈α
process␈α
in␈α
terms␈α
of␈α
␈↓α*plus␈↓␈α
is␈α
called␈α
␈↓↓macro␈α
expansion␈↓.␈α
Notice␈α
that␈α
is␈α
macro
␈↓expansion␈α∞can␈α
be␈α∞done␈α
by␈α∞a␈α
compiler,␈α∞generating␈α∞a␈α
sequence␈α∞of␈α
calls␈α∞on␈α
␈↓α*plus␈↓.␈α∞ Realize␈α∞too,␈α
that
␈↓since␈α⊃LISP␈α⊃programs␈α⊂must␈α⊃perform␈α⊃equivalently␈α⊂when␈α⊃interpreted,␈α⊃we␈α⊂must␈α⊃recognize␈α⊃a␈α⊂macro
␈↓construction␈α
inside␈α
␈↓αeval␈↓.

␈↓How␈α
are␈α
macros␈α∞written␈α
and␈α
how␈α
do␈α∞they␈α
work?␈α
 The␈α∞body␈α
of␈α
a␈α
macro␈α∞is␈α
a␈α
λ-expression␈α∞of␈α
␈↓↓one␈↓
␈↓argument.␈αThe␈α
␈↓↓use␈↓␈αof␈αa␈α
macro␈αlooks␈αjust␈α
like␈αan␈αordinary␈α
function␈αcall,␈αbut␈α
what␈αis␈αbound␈α
to␈αthe
␈↓λ-variable␈αis␈αthe␈αwhole␈αcall␈α
on␈αthe␈αmacro.␈α When␈αyou␈α
look␈αat␈αthe␈αevaluation␈αmechanism␈αfor␈α
macros
␈↓in␈αthe␈α␈↓	SM␈↓-␈↓αeval␈↓␈αyou␈αwill␈αsee␈αthat␈αthe␈αresult␈αof␈αthe␈αmacro␈αexpansion␈αis␈αpassed␈αto␈α␈↓αeval␈↓.␈αThus␈αthe␈αtask
␈↓of␈αthe␈αmacro␈αbody␈αis␈αto␈αexpand␈αthe␈αmacro␈αcall␈αand␈αreturn␈αthis␈αexpansion␈αas␈αits␈αvalue.␈α The␈αtask␈αof
␈↓the␈α
compiler␈αwill␈α
be␈αquite␈α
similar;␈αit␈α
will␈α
expand␈αthe␈α
macro␈αbut␈α
instead␈αof␈α
evaluating␈αthe␈α
expansion
␈↓it␈α
must␈α
␈↓↓compile␈↓␈α
it.
␈↓␈↓↓120  symbol tables␈↓ )6.13␈↓


␈↓Let's␈αdefine␈α␈↓α<␈↓βm␈↓α=␈↓␈α
to␈αmean␈α" is defined to be the macro ...".␈α Then␈α
a␈αsimple␈αmacro␈αdefinition␈α
of␈α␈↓αplus␈↓
␈↓might␈α
be:

␈↓α␈↓ αXplus <␈↓βm␈↓α= λ[[l]␈↓ ∧H[eq[length[l];3] → cons[*PLUS;cdr[l]]
␈↓α␈↓ αX␈↓ ∧H T → list[*PLUS;cadr[l];cons[PLUS;cddr[l]]]] ]

␈↓Thus␈αa␈α
call␈α␈↓α(PLUS␈α3␈α
4␈α5)␈↓␈αwould␈α
bind␈α␈↓αl␈↓␈αto␈α
␈↓α(PLUS␈α3␈α4␈α
5)␈↓␈αand␈αthe␈α
evaluation␈αof␈αthe␈α
body␈αwould
␈↓result␈α
in␈α
␈↓α(*PLUS␈α
3␈α(PLUS␈α
4␈α
5))␈↓.␈α
 Evaluation␈αof␈α
this␈α
expression␈α
would␈αresult␈α
in␈α
another␈α
call␈αon␈α
the
␈↓macro.␈α∂ This␈α∞time␈α∂␈↓αl␈↓␈α∞would␈α∂be␈α∂bound␈α∞to␈α∂␈↓α(PLUS␈α∞4␈α∂5)␈↓.␈α∂Now␈α∞␈↓αeq[length[l];3]␈↓␈α∂␈↓↓is␈↓␈α∞true␈α∂and␈α∂the␈α∞value
␈↓returned␈α⊂is␈α⊂␈↓α(*PLUS␈α⊃4␈α⊂5)␈↓.␈α⊂ This␈α⊃can␈α⊂be␈α⊂evaluated,␈α⊃giving␈α⊂9,␈α⊂and␈α⊃finally␈α⊂the␈α⊂outermost␈α⊃call␈α⊂on
␈↓␈↓α*PLUS␈↓␈α
has␈α
all␈α
its␈α
arguments␈α
evaluated,␈α
and␈α
we␈α
get␈α
the␈α
final␈α
answer,␈α
12.

␈↓This␈α
is␈α
a␈α
very␈α
simple␈α(and␈α
inefficient)␈α
macro␈α
definition.␈α
Since␈α
the␈αbody␈α
of␈α
a␈α
macro␈α
has␈αavailable␈α
all
␈↓of␈α∞the␈α∞evaluation␈α∞mechanism␈α
of␈α∞LISP,␈α∞and␈α∞since␈α
the␈α∞program␈α∞structure␈α∞of␈α
LISP␈α∞is␈α∞also␈α∞the␈α
data
␈↓structure,␈α
we␈α
can␈α
perform␈α∞arbitrary␈α
computations␈α
inside␈α
the␈α∞expansion␈α
of␈α
the␈α
macro.␈α∞Truly␈α
LISP
␈↓macros␈α
have␈α
the␈α
power␈α
to␈α
cloud␈α
men's␈α
minds!!!

␈↓Notice␈α
that␈α
␈↓αSETQ␈↓␈α
can␈α
easily␈α
be␈α
defined␈α
as␈α
a␈α
macro␈α
over␈α
␈↓αSET␈↓:

␈↓α␈↓ βqsetq <␈↓βm␈↓α= λ[[m] list[SET;list[QUOTE;cadr[l]];caddr[l]]].

␈↓The␈αeffect␈αof␈α"<␈↓βm␈↓="␈αshould␈αbe␈αclear:␈αit␈αputs␈αthe␈αbody␈αof␈αthe␈αmacro␈αdefinition␈αon␈αthe␈αproperty-list␈αof
␈↓the␈α
macro␈αname␈α
under␈α
the␈αindicator,␈α
␈↓αMACRO␈↓.␈αLikewise␈α
"<="␈α
puts␈αthe␈α
function␈αbody␈α
on␈α
the␈αp-list
␈↓under␈α∂the␈α∞indicator,␈α∂␈↓αEXPR␈↓.␈α∞Similarly,␈α∂we␈α∞will␈α∂define␈α∞"<␈↓βf␈↓="␈α∂to␈α∞mean␈α∂"..is␈α∞defined␈α∂to␈α∞be␈α∂a␈α∞special
␈↓form..."␈α
and␈α∞whose␈α
effect␈α
is␈α∞to␈α
put␈α
the␈α∞body␈α
under␈α
the␈α∞indicator,␈α
␈↓αFEXPR␈↓.␈α
 The␈α∞body␈α
of␈α∞a␈α
fexpr
␈↓definition␈αis␈αa␈αλ-expression␈αof␈α(usually)␈αa␈αsingle␈α
λ-variable,␈αsay␈α␈↓αl␈↓.␈αWhen␈αthe␈αfexpr␈αis␈αcalled␈αwe␈α
bind
␈↓the␈α
list␈α
of␈α
␈↓↓unevaluated␈↓␈α
arguments␈α
to␈α
␈↓αl␈↓.␈α
 Thus␈α
we␈α
could␈α
define␈α
␈↓αplus␈↓␈α
by:

␈↓αplus <␈↓βf␈↓α= λ[␈↓ αX[l] prog[[sum]
␈↓α␈↓ αX␈↓ αxsum ← 0;
␈↓α␈↓ αXa␈↓ αx[null[l] → return[sum]];
␈↓α␈↓ αX␈↓ αxsum ← *plus[sum;eval[car[l]]];
␈↓α␈↓ αX␈↓ αxl ← cdr[l];
␈↓α␈↓ αX␈↓ αxgo[a]]]


␈↓Or␈α
␈↓αand␈↓␈α
could␈α
be␈α
defined␈α
as:

␈↓α␈↓ ¬Eand <␈↓βf␈↓α= λ[[l]evand[l]]␈↓

␈↓where ␈↓αevand␈↓ is defined as:

␈↓α␈↓ βxevand <= λ[[l][␈↓ ¬hnull[l] → T;
␈↓α␈↓ βx␈↓ ¬heval[car[l]] → evand[cdr[l]];
␈↓α␈↓ βx␈↓ ¬hT → NIL]]
␈↓␈↓↓6.13␈↓ λ Macros and special forms     121␈↓


␈↓Notice␈α⊃that␈α⊃this␈α∩definition␈α⊃of␈α⊃␈↓αevand␈↓␈α⊃differs␈α∩from␈α⊃the␈α⊃one␈α⊃on␈α∩page␈α⊃65.␈α⊃ The␈α∩earlier␈α⊃definition
␈↓required␈αan␈αexplicit␈αsymbol␈αtable;␈αthe␈αnewer␈αone␈αuses␈αthe␈αglobal␈αtable.␈αThis␈αis␈αa␈αmixed␈αblessing.␈αAs
␈↓usual␈α∩there␈α∩are␈α∩difficulties␈α∩in␈α∩getting␈α∩the␈α∩correct␈α∩bindings␈α∩of␈α∩variables.␈α∩ Most␈α∩applications␈α∩of
␈↓␈↓αFEXPR␈↓s␈α∞involve␈α∞explicit␈α∞calls␈α∞on␈α∞␈↓αeval␈↓␈α∞within␈α
the␈α∞body␈α∞of␈α∞the␈α∞␈↓αFEXPR␈↓.␈α∞ Consider␈α∞the␈α
following
␈↓sequence␈α
:
␈↓α␈↓ βxwrong <␈↓βf␈↓α= λ[[x] prog[[y]
␈↓α␈↓ βx␈↓ ελy ← 2;
␈↓α␈↓ βx␈↓ ελreturn[eval[car[x]]]]

␈↓α␈↓ βxy ← 0;
␈↓α␈↓ βxwrong[y];

␈↓Execution␈αof␈α
the␈αabove␈α
sequence␈αshow␈αthe␈α
value␈αof␈α
␈↓αwrong[y]␈↓␈αto␈αbe␈α
␈↓α2␈↓,␈αrather␈α
than␈αthe␈α
expected␈α␈↓α0␈↓.
␈↓Clearly,␈αthe␈α
problem␈αis␈α
that␈αthe␈αcall␈α
on␈α␈↓αeval␈↓␈α
takes␈αplace␈αin␈α
the␈αwrong␈α
environment.␈αTo␈αalleviate␈α
this
␈↓situation␈α∞␈↓αFEXPR␈↓s␈α∞may␈α∞be␈α∞defined␈α∞with␈α∞␈↓↓two␈↓␈α∞arguments.␈α
In␈α∞this␈α∞case␈α∞a␈α∞␈↓↓call␈↓␈α∞on␈α∞the␈α∞␈↓αFEXPR␈↓␈α
will
␈↓bind␈αthe␈αenvironment␈α␈↓↓at␈αthe␈αpoint␈αof␈αcall␈↓␈αto␈αthat␈αsecond␈αargument.␈α␈↓αeval␈↓,and␈α␈↓αapply␈↓␈αare␈αallowed␈αto
␈↓be␈α⊂called␈α⊂with␈α∂either␈α⊂one␈α⊂or␈α∂two␈α⊂arguments.␈α⊂If␈α∂a␈α⊂second␈α⊂argument␈α∂is␈α⊂present,␈α⊂it␈α∂is␈α⊂used␈α⊂as␈α∂the
␈↓environment␈α
during␈α
evaluation.␈α
 Thus:
␈↓α␈↓ βxright <␈↓βf␈↓α= λ[[x;a] prog[[y]
␈↓α␈↓ βx␈↓ ε(y ← 2;
␈↓α␈↓ βx␈↓ ε(return[eval[car[x];a]]]

␈↓α␈↓ βxy ← 0;
␈↓α␈↓ βxright[y];

␈↓The␈α
call␈α
on␈α
␈↓αright␈↓␈α
will␈α
use␈α
the␈α
environment␈α
with␈α
␈↓αy␈↓␈α
being␈α
␈↓α0␈↓␈α
rather␈α
than␈α
␈↓α2␈↓.

␈↓Macros␈α∂can␈α∂be␈α∂used␈α⊂to␈α∂perform␈α∂the␈α∂operations␈α∂which␈α⊂special␈α∂forms␈α∂do,␈α∂but␈α∂with␈α⊂perhaps␈α∂more
␈↓efficiency.␈α∪The␈α∪idea␈α∩of␈α∪macro␈α∪processing␈α∪is␈α∩not␈α∪recent.␈α∪ Some␈α∩of␈α∪the␈α∪earliest␈α∪assemblers␈α∩had
␈↓extensive␈α
macro␈α
facilities.␈α∞ Lately␈α
macros␈α
have␈α∞been␈α
used␈α
as␈α
a␈α∞means␈α
of␈α
extending␈α∞so-called␈α
high
␈↓level␈α
languages.

␈↓One␈αof␈α
the␈αmost␈α
simple␈αkinds␈αof␈α
macros␈αis␈α
␈↓↓textual␈αsubstitution␈↓.␈α
 That␈αis,␈αwhen␈α
a␈αuse␈α
of␈αa␈αmacro␈α
is
␈↓detected␈α∂we␈α⊂simply␈α∂replace␈α∂the␈α⊂call␈α∂by␈α∂its␈α⊂body.␈α∂ A␈α∂slightly␈α⊂more␈α∂sophisticated␈α∂appliation␈α⊂is␈α∂the
␈↓␈↓↓syntax␈αmacro␈↓.␈α
 Everytime␈αwe␈α
come␈αacross␈α
an␈αapplication␈α
of␈αa␈α
syntax␈αmacro␈α
the␈αexpander␈α
processes
␈↓it␈α
as␈αif␈α
it␈αhad␈α
never␈αbeen␈α
seen␈α
before␈αeven␈α
though␈αmuch␈α
of␈αthe␈α
expansion␈αis␈α
repetitious.␈α
That␈αis,
␈↓syntax␈α
macros␈α
have␈α
no␈α
memory.

␈↓␈↓↓computational␈αmacros␈↓␈α
are␈αan␈α
attempt␈αto␈α
reduce␈αsome␈α
of␈αthis␈α
repetition.␈α In␈α
this␈αscheme␈α
a␈αcertain
␈↓amount␈α⊂of␈α⊃processing␈α⊂is␈α⊃done␈α⊂at␈α⊃the␈α⊂time␈α⊃the␈α⊂macro␈α⊃is␈α⊂␈↓↓defined␈↓.␈α⊃For␈α⊂example␈α⊃a␈α⊂computational
␈↓subtree␈α
refelcting␈αthe␈α
body␈αof␈α
the␈αmacro␈α
might␈αbe␈α
formed.␈α Then␈α
whenever␈αthe␈α
macro␈αis␈α
␈↓↓used␈↓␈αwe
␈↓can␈α∞simply␈α∞make␈α∞a␈α
copy␈α∞of␈α∞this␈α∞subtree␈α
and␈α∞"glue"␈α∞this␈α∞subtree␈α
into␈α∞the␈α∞parse-tree␈α∞which␈α∞we␈α
are
␈↓building.␈α⊃ This␈α⊃computational␈α⊃subtree␈α⊃is␈α⊃commonly␈α⊃formed␈α⊃by␈α⊃passing␈α⊃the␈α⊃body␈α⊃of␈α∩the␈α⊃macro
␈↓through␈αthe␈αcompiler␈α
in␈αa␈α"funny"␈αway.␈α
 The␈αmain␈αproblem␈αwith␈α
the␈αcomputational␈αmacro␈α
is␈αthat
␈↓there␈α∩are␈α∩many␈α∪desirable␈α∩macros␈α∩which␈α∪have␈α∩no␈α∩such␈α∩subtree,␈α∪or␈α∩there␈α∩is␈α∪other␈α∩information
␈↓␈↓↓122  symbol tables␈↓ )6.13␈↓


␈↓necessary␈αto␈αprocess␈αthe␈αmacro.␈α There␈αare␈αsolutions␈αto␈αthis␈αproblem,␈αone␈αof␈αwhich␈αclosely␈αparallels
␈↓the␈α⊂abstract␈α⊃syntax␈α⊂ideas␈α⊃of␈α⊂McCarthy.␈α⊃ All␈α⊂of␈α⊂these␈α⊃styles␈α⊂of␈α⊃macros␈α⊂are␈α⊃subsets␈α⊂of␈α⊃the␈α⊂LISP
␈↓macros.

␈↓Many␈αversions␈αof␈αLISP␈αalso␈αhave␈αa␈αvery␈αsimple␈αsyntax␈αmacro␈α(called␈α␈↓αread␈↓␈αmacros)␈αindependent␈αof
␈↓the␈α<␈↓βm␈↓=␈α-␈α
facility.␈αConstants␈αappear␈αquite␈α
frequently␈αin␈αLISP␈αexpressions;␈α
and␈αso␈αwe␈α
have␈αalready
␈↓introduced␈α
abbreviations␈α
for␈α
the␈α
sexpression␈α
representation␈α
of␈α
numbers,␈α
␈↓αT␈↓␈α
and␈α
␈↓αNIL␈↓.␈α
 That␈α
is␈α
we
␈↓don't␈α
␈↓αQUOTE␈↓␈α
them.␈α
 ␈↓αread␈↓␈α
macros␈α
are␈α
used␈α
to␈α
abbreviate␈α
other␈α
sexpressions.␈α
The␈α
most␈αcommon␈α
␈↓αread␈↓
␈↓macro␈α
is␈α
used␈α
to␈α
abbreviate␈α
the␈α
construct:

␈↓␈↓ ¬0␈↓α(QUOTE ␈↓<sexpression>␈↓α)␈↓. 

␈↓A␈αspecial␈α
character␈αis␈αchosen␈α
to␈αdesignate␈αthe␈α
macro,␈αsay␈α"@".␈α
Then␈αthen␈αwhenever␈α
␈↓αread␈↓␈αsees␈α"@"␈α
the
␈↓next␈α
sexpression,␈α
s,␈α
is␈α
read␈α
in␈α
and␈α
the␈α
value:␈α
␈↓α(QUOTE␈α
␈↓s␈↓α)␈↓␈α
is␈α
returned␈α
by␈α
␈↓αread␈↓.␈α
Thus:

␈↓␈↓ β9@<sexpression> is an abbreviation for ␈↓α(QUOTE <sexpression>).␈↓ 

␈↓In␈α
some␈α
systems␈α
the␈α
user␈α
may␈α
define␈α
his␈α
own␈α
␈↓αread␈↓␈α
macros,␈α
in␈α
others␈α
they␈α
are␈α
pre-defined.



␈↓␈↓ ¬n␈↓↓6.14  Problems␈↓


␈↓I.␈α Define␈α␈↓αlist␈↓␈αand␈α␈↓αappend␈↓␈αas␈αmacros.␈αYou␈αmay␈αuse␈αonly␈αthe␈αLISP␈αprimitives␈α(functions,␈αpredicates,
␈↓conditionals␈α
and␈α
recursion)␈α
and␈α
a␈α
binary␈α
function,␈α
␈↓α*append␈↓.

␈↓II.␈α,Give␈α,a␈α,macro␈α,definition␈α-of␈α,an␈α,extended␈α,␈↓αSETQ␈↓,␈α,which␈α,is␈α-called␈α,as
␈↓␈↓α(SETQ ␈↓var␈↓β1␈↓ exp␈↓β1␈↓ ... var␈↓βn␈↓ exp␈↓βn␈↓α)␈↓.␈α∞ Each␈α∞var␈↓βi␈↓␈α∞is␈α∞a␈α∞name;␈α∞each␈α∞exp␈↓βi␈↓␈α∞is␈α∞an␈α∞expression␈α∞to␈α∞be␈α∞evaluated
␈↓and␈α
assigned␈α
to␈α
var␈↓βi␈↓.␈α
The␈α
assignments␈α
should␈α
go␈α
from␈α
"left-to-right".

␈↓Thus␈α
␈↓α(SETQ␈α
X␈α
2␈α
Y␈α
(TIMES␈α
2␈α
X)␈α
X␈α
3)␈↓␈α
when␈α
executed␈α
should␈α
assign␈α
␈↓α3␈↓␈α
to␈α
␈↓αX␈↓␈α
and␈α
␈↓α4␈↓␈α
to␈α
␈↓αY␈↓.

␈↓III.␈α
Define␈α
␈↓αlist␈↓␈α
as␈α
a␈α
fexpr.



␈↓␈↓ ¬y␈↓↓6.15  ␈↓	SM␈↓↓-␈↓αeval␈↓↓␈↓α


␈↓Finally,␈α
here␈α
is␈α
the␈α
new␈α
evaluator.

␈↓(the␈α
`line␈α
numbers'␈α
are␈α
not␈α
part␈α
of␈α
the␈α
definitions)
␈↓αeval <= 
␈↓α1. λ[[x]prog[[y]
␈↓α2.      return[[numberp[x] → x;
␈↓␈↓↓6.15␈↓ 
⊂␈↓	SM␈↓↓-␈↓αeval␈↓↓     123␈↓α


␈↓α3.              atom[x] → [y ← get[car[x];VALUE] → cdr[y]]
␈↓α                           T → err[UNBOUND VARIABLE]];
␈↓α4.              atom[car[x]] → [y ← getl[car[x];(EXPR FEXPR MACRO)]
␈↓α5.                                → [eq[car[y];EXPR] → apply[cadr[y];mapcar[function[eval];cdr[x]]];
␈↓α6.                                   eq[car[y];FEXPR] → apply[cadr[y];list[cdr[x]]];
␈↓α7.                                   T → eval[apply[cadr[y];list[x]]]];
␈↓α8.                              y ← get[car[x];VALUE] → eval[cons[cdr[y];cdr[x]]];
␈↓α9.                              T → err[UNDEFINED FUNCTION]];
␈↓α10.             T → apply[car[x];mapcar[function[eval];cdr[x]]]]]]]


␈↓αapply <=
␈↓α λ[[fn;args]
␈↓α11.     [atom[fn] → [get[fn;EXPR] → apply[get[fn;EXPR];args];
␈↓α12.                  T → apply[eval[fn];args]];
␈↓α13.      eq[car[fn;LAMBDA] → prog[[z]
␈↓α14.                             bind[cadr[fn];args];
␈↓α15.                             z ← eval[caddr[fn]];
␈↓α16.                             unbind[];
␈↓α17.                             return[z]];
␈↓α18.      T → apply[eval[fn];args] ]]


␈↓First␈α
let's␈α
describe␈α
the␈α
new␈α
LISP␈α
system␈α
functions␈α
which␈α
are␈α
used␈α
here.␈α
 They␈α
are:

␈↓␈↓αget[x;y]:␈α␈↓␈α␈↓αx␈↓␈αis␈αan␈αatom;␈α␈↓αy␈↓␈αis␈α
an␈αindicator.␈α ␈↓αget␈↓␈αwill␈αreturn␈αthe␈αvalue-part␈αof␈αthe␈α
attribute-value␈αpair
␈↓␈↓ αhassociated␈α
with␈α
␈↓αy␈↓␈α
under␈α
the␈α
atom␈α
␈↓αx␈↓.␈α
 If␈α
␈↓αx␈↓␈α
does␈α
not␈α
have␈α
the␈α
indicator␈α
␈↓αy␈↓,␈α
then␈α∞␈↓αNIL␈↓␈α
is
␈↓␈↓ αhreturned.

␈↓␈↓αgetl[x;y]:␈↓␈α
␈↓αx␈↓␈αis␈α
an␈αatom;␈α
␈↓αy␈↓␈αis␈α
a␈αlist␈α
of␈αindicators␈α
(see␈αline␈α
4).␈α␈↓αgetl␈↓␈α
will␈αsearch␈α
the␈αproperty␈α
list␈α
of␈αthe
␈↓␈↓ αhatom␈α
␈↓αx␈↓␈αfor␈α
the␈α
first␈αoccurrence␈α
of␈α
an␈αindicator␈α
which␈α
appears␈αin␈α
the␈α
list␈α␈↓αy␈↓.␈α
 If␈α
such␈αa
␈↓␈↓ αhmatch␈α∃is␈α∀found,␈α∃then␈α∀the␈α∃remainder␈α∀of␈α∃the␈α∀p-list,␈α∃beginning␈α∀with␈α∃the␈α∀matching
␈↓␈↓ αhindicator,␈α
is␈α
returned.␈α
 If␈α
no␈α
match␈α
is␈α
found,␈α
␈↓αNIL␈↓␈α
is␈α
returned.
␈↓An example:






␈↓␈↓ α{␈↓αgetl[FOO;(BAZ PNAME)]␈↓ has value:      ␈↓αget[FOO;PNAME]␈↓ has value:

␈↓The␈α
virtue␈α
of␈α
␈↓αgetl␈↓␈α
is␈α
that␈α
it␈α
can␈α∞distinguish␈α
between␈α
an␈α
atom␈α
which␈α
has␈α
an␈α
indicator␈α∞with␈α
value
␈↓␈↓αNIL␈↓␈α
and␈α
an␈α
atom␈α
which␈α
does␈α
not␈α
have␈α
the␈α
indicator.␈α
 ␈↓αget␈↓␈α
cannot␈α
communicate␈α
this␈α
distinction.
␈↓␈↓↓124  symbol tables␈↓ (6.15␈↓


␈↓First,␈αon␈αlines␈α5␈α
and␈α10␈αwe␈αuse␈α
the␈αLISP␈αmapping␈αfunction,␈α
␈↓αmapcar␈↓,␈αto␈αevaluate␈αthe␈α
argument␈αlist
␈↓during␈α
function␈α
calls.␈α
See␈α
page␈α
61.

␈↓Notice␈αline␈α␈↓α3.␈↓␈αis␈αan␈αapplication␈αof␈αan␈αobscene␈αLISP␈αcoding␈αtrick␈αdescribed␈αon␈αpage␈α92.␈α
Notice␈αtoo,
␈↓that␈α
line␈α
␈↓α3␈↓␈α
uses␈α
␈↓αget␈↓.␈α
You␈α
should␈α
understand␈α
why␈α
␈↓αget␈↓␈α
works␈α
and␈α
␈↓αgetl␈↓␈α
is␈α
not␈α
required.

␈↓There␈α∂are␈α∞enough␈α∂important␈α∂new␈α∞details␈α∂in␈α∞this␈α∂␈↓αeval-apply␈↓␈α∂family␈α∞to␈α∂warrant␈α∞spending␈α∂a␈α∂bit␈α∞of
␈↓time.␈α∞Most␈α∞of␈α∞the␈α∞changes␈α∞involve␈α∞symbol␈α∞table␈α∞operations.␈α∞ ␈↓αeval␈↓␈α∞(and␈α∞␈↓αapply␈↓)␈α∞no␈α∞longer␈α∞carry␈α
an
␈↓explicit␈α∞symbol␈α∂table.␈α∞ The␈α∂property-list␈α∞of␈α∂tha␈α∞atom␈α∞contains␈α∂the␈α∞information␈α∂now.␈α∞ In␈α∂this␈α∞new
␈↓scheme␈α
of␈α
things,␈α
␈↓αget␈↓␈α
and␈α
␈↓αgetl␈↓␈α
search␈α
the␈α
symbol␈α
table␈α
(p-list)␈α
for␈α
the␈α
appropriate␈α
bindings.

␈↓␈↓αeval␈↓␈α⊃first␈α⊂checks␈α⊃if␈α⊂it␈α⊃has␈α⊂been␈α⊃given␈α⊂a␈α⊃number;␈α⊂any␈α⊃other␈α⊂atomic␈α⊃expression␈α⊂given␈α⊃to␈α⊃␈↓αeval␈↓␈α⊂is
␈↓expected␈α
to␈α
be␈α
a␈α
variable␈α
and␈α
to␈α
have␈α
a␈α
␈↓αVALUE␈↓-cell.

␈↓If␈αthe␈α
␈↓αcar␈↓␈αof␈αthe␈α
expression␈αgiven␈α
to␈α␈↓αeval␈↓␈αis␈α
atomic,␈αthen␈α
the␈αp-list␈αof␈α
that␈αatom␈α
is␈αexamined␈αfor␈α
one
␈↓of␈α∂three␈α∂indicators.␈α∂ We␈α∞have␈α∂already␈α∂seen␈α∂␈↓αEXPR␈↓␈α∞on␈α∂page␈α∂89,␈α∂it␈α∞is␈α∂the␈α∂indicator␈α∂designating␈α∞a
␈↓function␈α⊃represented␈α⊂as␈α⊃a␈α⊂λ-expression.␈α⊃ Evaluate␈α⊂the␈α⊃arguments␈α⊂and␈α⊃call␈α⊂␈↓αapply␈↓.␈α⊃The␈α⊂indicator
␈↓␈↓αFEXPR␈↓,␈α∞designates␈α∞a␈α
special␈α∞form.␈α∞Pass␈α
the␈α∞␈↓↓un␈↓evaluated␈α∞argument␈α
list␈α∞to␈α∞␈↓αapply␈↓.␈α∞ The␈α
indicator,
␈↓␈↓αMACRO␈↓,␈α
is␈α
recognized␈α
on␈α
line␈α
␈↓α7␈↓.

␈↓There␈αare␈αactually␈αtwo␈αother␈αindicators␈αrecognized␈αby␈α␈↓αeval␈↓.␈α␈↓αSUBR␈↓␈αis␈αthe␈αmachine-language␈αversion
␈↓of␈α
an␈α
␈↓αEXPR␈↓;␈α
and␈α
␈↓αFSUBR␈↓␈α
is␈α
the␈α
machine␈α
version␈α
of␈α
a␈α
␈↓αFEXPR␈↓.



␈↓␈↓ ¬;␈↓↓6.16  Binding revisited␈↓


␈↓The␈α
most␈α
interesting␈α
changes␈α
in␈α
the␈α
evaluator␈α
involve␈α
binding␈α
and␈α
unbinding␈α
of␈α
variables.

␈↓We␈αhave␈αseen␈αthat␈αthe␈α
old␈αsymbol␈αtable␈αmechanism␈αhas␈α
the␈αcorrect␈αeffect␈αfor␈αthe␈αproper␈α
evaluation
␈↓of␈αrecursive␈αfunctions.␈α As␈αwe␈αbound␈αvariables␈αto␈αvalues␈αon␈αentry␈αto␈αa␈αλ-expression,␈αwe␈α␈↓αcons␈↓ed␈αthe
␈↓new␈α
bindings␈α
onto␈α
the␈α
front␈α
of␈α
the␈α
symbol␈α
table.␈α
 This␈α
had␈α
two␈α
results:

␈↓␈↓↓1.␈↓␈α
In␈α
the␈α
evaluation␈α
of␈α
the␈α
body␈α
of␈α
the␈α
λ-expression␈α
we␈α
got␈α
the␈α
correct␈α
bindings␈α
of␈α
the␈α
variables.

␈↓␈↓↓2.␈↓␈α
The␈α
old␈α
value␈α
was␈α
saved␈α
so␈α
that␈α
we␈α
could␈α
retrieve␈α
it␈α
on␈α
leaving␈α
the␈α
λ-body.

␈↓Now␈αwith␈αthe␈αnew␈αtable␈αorganization␈αwe␈αneed␈αto␈αdevelop␈αthe␈αsame␈αfacility.␈α It␈αis␈αnot␈αsufficient␈αfor
␈↓the␈α∞binding␈α∞of␈α∞λ-variables␈α∂to␈α∞simply␈α∞change␈α∞the␈α∞contents␈α∂of␈α∞the␈α∞␈↓αVALUE␈↓-cell.␈α∞This␈α∂would␈α∞violate
␈↓condition␈α
␈↓↓2␈↓.␈α
Besides␈α
changing␈α
the␈α
value,␈α
we␈α
must␈α
save␈α
the␈α
prior␈α
value.

␈↓The␈α
crucial␈αphrases␈α
are␈αlines␈α
14␈αand␈α
16␈αin␈α
the␈αnew␈α
version␈αof␈α
␈↓αapply␈↓.␈α ␈↓αbind␈↓␈α
is␈αa␈α
function,␈αtaking␈α
two
␈↓arguments.␈α The␈αfirst␈αis␈αthe␈αlist␈αof␈αλ-variables;␈αthe␈αsecond␈αis␈αthe␈αlist␈αof␈αnew␈αvalues.␈α ␈↓αbind␈↓␈αsaves␈αthe
␈↓old␈α
values␈α∞and␈α
rebinds␈α∞the␈α
λ-variables␈α∞to␈α
the␈α
new␈α∞values.␈α
 ␈↓αunbind␈↓␈α∞is␈α
a␈α∞function␈α
of␈α∞no␈α
arguments
␈↓␈↓↓6.16␈↓ 	∪Binding revisited     125␈↓


␈↓used␈α∀to␈α∀restore␈α∀a␈α∀list␈α∀of␈α∀λ-variables␈α∃to␈α∀their␈α∀previous␈α∀values.␈α∀ How␈α∀is␈α∀this␈α∃wonderous␈α∀feat
␈↓performed?

␈↓We␈α∞have␈α∞a␈α∞stack,␈α∞or␈α∞pushdown-list,␈α∞called␈α∞SP␈α
(for␈α∞␈↓↓S␈↓pecial␈α∞␈↓↓P␈↓ushdown)␈α∞in␈α∞which␈α∞we␈α∞can␈α∞save␈α
old
␈↓values␈αof␈αvariables.␈α ␈↓αbind␈↓␈αwill␈α
add␈αvalues␈αto␈αSP;␈α␈↓αunbind␈↓␈α
restores␈αthe␈αsaved␈αvalues.␈α Actually␈α
because
␈↓of␈αthe␈αstack-like␈αbehavior␈αof␈αthe␈αbinding␈α
and␈αunbinding␈αprocesses␈αwe␈αcan␈αincrease␈αthe␈αefficiency␈α
of
␈↓␈↓αbind␈↓␈αand␈α␈↓αunbind␈↓.␈α ␈↓αbind␈↓␈αsaves␈αboth␈αthe␈αvalue␈αand␈αthe␈αlocation␈αof␈αthe␈α␈↓αVALUE␈↓-cell␈αfor␈αeach␈αvariable
␈↓being␈α
rebound.␈α
 It␈α∞also␈α
puts␈α
special␈α
marks␈α∞in␈α
the␈α
SP␈α
stack␈α∞delimiting␈α
each␈α
block␈α∞of␈α
λ-rebindings.
␈↓All␈α␈↓αunbind␈↓␈αneed␈αdo␈αis␈αrestore␈αthe␈αfirst␈α
block␈αof␈αsaved␈αvalues.␈α It␈αknows␈αthe␈αvalues␈αand␈α
also␈αknows
␈↓the␈αaddresses␈αof␈αthe␈α␈↓αVALUE␈↓-cells.␈α All␈αof␈αthe␈αinformation␈αit␈αneeded␈αfor␈αrestoration␈αis␈αsaved␈α
in␈αthe
␈↓stack.

␈↓Given λ[[x␈↓β1␈↓; ...; x␈↓βn␈↓]␈↓λx␈↓][a␈↓β1␈↓; ... ;a␈↓βn␈↓], here's an example of binding:


␈↓␈↓ ¬_        to value␈↓ πλ␈↓ λxold value of x␈↓β1␈↓
␈↓␈↓ ¬_       cell for x␈↓β1␈↓

␈↓␈↓ ¬_save
␈↓␈↓ ¬_and␈↓ πλ  ...
␈↓␈↓ ¬_rebind
␈↓␈↓ ¬_  ===>

␈↓␈↓ ¬_        to value␈↓ πλ␈↓ λxold value of x␈↓βn␈↓
␈↓␈↓ ¬_       cell for x␈↓βn␈↓





␈↓␈↓ ¬_␈↓ πλ             |
␈↓␈↓ ¬_␈↓ πλ             ↓
␈↓␈↓ ¬_␈↓ πλrebind x␈↓βi␈↓ to a␈↓βi␈↓, changing the
␈↓␈↓ ¬_␈↓ πλ  contents of the value cell
␈↓␈↓ ¬_␈↓ πλ      (see ␈↓α*bind␈↓)

␈↓␈↓ ¬_␈↓ πλ             |
␈↓␈↓ ¬_␈↓ πλ             ↓
␈↓␈↓ ¬_␈↓ πλ        evaluate ␈↓λx␈↓

␈↓␈↓ ¬_␈↓ πλ             |
␈↓␈↓ ¬_␈↓ πλ             ↓
␈↓␈↓ ¬_␈↓ πλrestore old values using information
␈↓␈↓ ¬_␈↓ πλ    stored in satck, SP.
␈↓␈↓ ¬_␈↓ πλ       (see ␈↓α*unbind␈↓)
␈↓␈↓↓126  symbol tables␈↓ )6.16␈↓

















␈↓␈↓ ∧i␈↓↓The primitives, ␈↓α*bind␈↓↓ and ␈↓α*unbind␈↓↓.

␈↓This␈αbinding␈α
scheme,␈αcalled␈α
␈↓↓shallow␈αbinding␈↓␈α
works␈αquite␈α
well␈αfor␈α
simple␈αλ-binding␈α
and␈αlookup.␈α
As
␈↓with␈α∩the␈α∪previous␈α∩a-list␈α∪symbol␈α∩table␈α∪we␈α∩need␈α∪excercise␈α∩some␈α∪care␈α∩when␈α∪handling␈α∩functional
␈↓arguments.␈αHow␈αcan␈αwe␈αimplement␈αthe␈αequivalent␈αof␈αthe␈α␈↓αFUNARG␈↓␈αhack␈αin␈αthis␈αnew␈αbinding␈αand
␈↓symbol␈α⊂table␈α⊂organization?␈α⊂ Recall␈α⊂how␈α⊂the␈α⊂old␈α∂␈↓αeval␈↓␈α⊂coped␈α⊂(page␈α⊂62).␈α⊂ When␈α⊂we␈α⊂recognized␈α∂an
␈↓instance␈α∩of␈α∩a␈α⊃functional␈α∩argument␈α∩we␈α∩saved␈α⊃a␈α∩pointer␈α∩to␈α⊃the␈α∩current␈α∩envirnoment.␈α∩When␈α⊃we
␈↓␈↓↓applied␈↓␈αthe␈αfunctional␈αargument␈α
we␈αrestored␈αthe␈αsymbol␈αtable␈α
in␈αsuch␈αa␈αway␈αthat␈α
global␈αvariables
␈↓were␈α↔accessed␈α⊗in␈α↔the␈α⊗saved␈α↔environment␈α⊗while␈α↔local␈α⊗variables␈α↔were␈α⊗found␈α↔in␈α↔the␈α⊗current
␈↓evnironment.␈α We␈αmust␈αdo␈αthe␈αsame.␈αWhen␈α␈↓αfunction␈↓␈αis␈αseen␈αsave␈αthe␈αcurrent␈αSP␈αpointer;␈αwhen␈αwe
␈↓apply␈α
the␈α
functional␈α
argument,

␈↓␈↓α␈↓ βx(FUNARG ␈↓<function> <old SP>␈↓α) ␈↓to     arg␈↓β1␈↓; ... arg␈↓βn␈↓α ,


␈↓we␈α∂will␈α∞first␈α∂rebind␈α∞all␈α∂of␈α∞the␈α∂variables␈α∂found␈α∞by␈α∂the␈α∞old␈α∂SP␈α∞pointer␈α∂to␈α∞those␈α∂old␈α∂values,␈α∞while
␈↓saving␈α
the␈α∞current␈α
values␈α∞in␈α
SP.␈α∞Then␈α
we␈α
can␈α∞rebind␈α
the␈α∞λ-variables␈α
(i.e.,␈α∞local␈α
variables)␈α∞of␈α
the
␈↓<function>␈αto␈αarg␈↓β1␈↓␈αthrough␈αarg␈↓βn␈↓.␈αThe␈αenvironment␈αwill␈αthen␈αbe␈αproperly␈αrestored␈αfor␈αevaluation␈αof
␈↓the␈α
function␈α
body.␈α
When␈α
the␈α
evaluation␈α
has␈α
been␈α
completed␈α
we␈α
restore␈α
using␈α
␈↓αunbind␈↓.

␈↓Compare␈α∂the␈α∂shallow␈α∂binding␈α∂strategy␈α∂to␈α∂that␈α∂of␈α∂the␈α∂a-list.␈α∂The␈α∂a-list␈α∂was␈α∂always␈α⊂explicit␈α∂when
␈↓evaluation␈α∩was␈α∩occurring;␈α∩saving␈α∩environments␈α∪only␈α∩required␈α∩saving␈α∩a␈α∩pointer␈α∩to␈α∪the␈α∩current
␈↓symbol␈α∞table;␈α∞changing␈α∞envirnoments␈α
at␈α∞most␈α∞required␈α∞calling␈α
␈↓αeval␈↓␈α∞with␈α∞a␈α∞different␈α∞symbol␈α
table,
␈↓and␈α⊂when␈α⊃leaving␈α⊂a␈α⊂context␈α⊃the␈α⊂old␈α⊂table␈α⊃was␈α⊂restored␈α⊂implicitly␈α⊃by␈α⊂the␈α⊃recursion␈α⊂mechanism.
␈↓Accessing␈α⊂a␈α⊂variable␈α⊂involved␈α⊂an␈α⊂appreciable␈α⊃computation;␈α⊂we␈α⊂had␈α⊂to␈α⊂search␈α⊂the␈α⊂table␈α⊃for␈α⊂the
␈↓variable-value␈α
pair.

␈↓Shallow␈α
binding␈α
obviates␈α
the␈α
symbol␈α
table␈α
search␈α
while␈α
incurring␈α
added␈α
expense␈α
in␈α∞binding␈α
and
␈↓unbinding.␈α The␈αcare␈αand␈αfeeding␈αof␈αfunctional␈αarguments␈αis␈αmore␈αdifficult␈αsince␈αthere␈αis␈αonly␈αone
␈↓symbol␈α⊂table,␈α⊂not␈α⊂the␈α∂tree␈α⊂of␈α⊂tables␈α⊂implicit␈α⊂in␈α∂the␈α⊂previous␈α⊂incarnation.␈α⊂True,␈α⊂the␈α∂information
␈↓necessary␈α
to␈α
recover␈α
an␈α
environment␈α
is␈α
present␈α
in␈α
SP,␈α
but␈α
it␈α
is␈α
expensive␈α
to␈α
retrieve␈α
it.
␈↓␈↓↓6.16␈↓ 	∪Binding revisited     127␈↓


␈↓Shallow␈α
binding:␈α
0;␈α
a-list:␈α
1;␈α
(Christians:␈α
0;␈α
lions:␈α
1).

␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓I␈α
Evaluate␈α
the␈α
following␈α
using␈α
the␈α
new␈α
␈↓αeval␈↓:

␈↓1.␈α
␈↓αeval[((LAMBDA(L)(PRINT␈α
L))(QUOTE␈α
A))]␈↓␈α
assuming␈α
␈↓αprint␈↓␈α
is␈α
the␈α
usual␈α
printing␈α
function.

␈↓2.␈α
␈↓αeval[(FOO␈α
(QUOTE␈α
A))]␈↓␈α
where␈α
␈↓αfoo␈α
<=␈α
λ[[l]print[l]]␈↓.

␈↓3.␈α
␈↓αeval[((CAR␈α
(QUOTE␈α
(FOO)))(QUOTE␈α
A))]␈↓,␈α
␈↓αfoo␈↓␈α
as␈α
above.

␈↓4.␈α
␈↓αeval[(FOO1␈α
(QUOTE␈α
A))]␈↓␈α
where␈α
␈↓αfoo1␈α
<␈↓βf␈↓α=␈α
λ[[l]print[l]]␈↓.

␈↓and␈α
finally:

␈↓5.␈α
␈↓αeval[((CAR␈α
(QUOTE␈α
(FOO1)))(QUOTE␈α
A))]␈↓.

␈↓Explain␈α
what␈α
happened.␈α
Can␈α
you␈α
"fix"␈α
it?

␈↓II␈α∂Some␈α∂implementations␈α⊂of␈α∂LISP␈α∂distinguish␈α∂between␈α⊂␈↓αEXPR␈↓s␈α∂and␈α∂␈↓αFEXPR␈↓s␈α∂by␈α⊂modifying␈α∂the
␈↓prefix,␈α∩␈↓αLAMBDA␈↓.␈α∩␈↓αFEXPR␈↓s␈α∩are␈α∩stored␈α∩with␈α∩prefix␈α∩␈↓αNLAMBDA␈↓;␈α∩␈↓αEXPR␈↓s␈α∩are␈α∩stored␈α∪with␈α∩the
␈↓normal␈α
prefix␈α
␈↓αLAMBDA␈↓.␈α
What␈α
are␈α
the␈α
advantages␈α
and/or␈α
disadvantages␈α
of␈α
this␈α
scheme.

␈↓III␈αRecall␈αthe␈αextensions␈αto␈αLISP␈αbinding␈αproposed␈αin␈αSection␈α5.2.␈α Discuss␈αtheir␈αimplementation␈α
in
␈↓light␈α
of␈α
the␈α
new␈α
␈↓αeval␈↓␈α
and␈α
symbol␈α
table.



␈↓␈↓ ¬}␈↓↓6.17  Review␈↓


␈↓As␈α∞things␈α∞presently␈α∞stand␈α∞we␈α∞have␈α∞a␈α∂basic␈α∞LISP␈α∞machine␈α∞consisting␈α∞of␈α∞an␈α∞encoding␈α∞of␈α∂␈↓αeval␈↓␈α∞and
␈↓friends,␈α∩the␈α∩I/O␈α⊃routines,␈α∩the␈α∩garbage␈α∩collector,␈α⊃an␈α∩initial␈α∩symbol␈α⊃table␈α∩which␈α∩believes␈α∩in␈α⊃the
␈↓primitive␈α
functions␈α
and␈α
some␈α
commonly␈α
needed␈αutility␈α
functions.␈α
 We␈α
have␈α
two␈α
areas␈α
of␈αmemory:
␈↓free␈α
space,␈α
and␈α
full␈α
word␈α
space.

␈↓Expressions␈α(forms)␈αto␈αbe␈αevaluated␈αare␈αread␈αin,␈αconverted␈αto␈αlist␈αstructure,␈αand␈αevaluated␈α
by␈α␈↓αeval␈↓.
␈↓What␈α∩␈↓αeval␈↓␈α∩is␈α∩doing␈α∩is␈α∩traversing␈α∪the␈α∩sexpression␈α∩representation␈α∩of␈α∩the␈α∩form,␈α∪interpreting␈α∩the
␈↓information␈αfound␈αthere␈αas␈αLISP␈α"instructions".␈α This␈αis␈αan␈αexpensive␈αprocess.␈α In␈αthe␈αgeneral␈αcase
␈↓there␈α∞is␈α∞nothing␈α
we␈α∞can␈α∞do␈α∞to␈α
reduce␈α∞this␈α∞cost,␈α
but␈α∞very␈α∞frequently␈α∞there␈α
is␈α∞a␈α∞mapping␈α∞from␈α
the
␈↓LISP␈αexpressions␈αto␈αbe␈α
evaluated␈αto␈αa␈αsequence␈αof␈α
actual␈αmachine␈αinstructions,␈αwhich␈αwhen␈α
carried
␈↓out␈αwill␈αhave␈αthe␈αsame␈αcomputational␈αeffect␈αas␈α␈↓αeval␈↓,␈αmassaging␈αthe␈αlist␈αstructure.␈α This␈αmapping␈αis
␈↓called␈α
a␈α
compiler.␈α
 ␈↓αeval␈↓␈α
and␈α
friends␈α
are␈α
called␈α
an␈α
␈↓αinterpreter␈↓.

␈↓What␈α
we␈α
wish␈α
to␈α
do␈α
now␈α
is␈α
describe␈α
a␈α
compiler␈α
for␈α
a␈α
subset␈α
of␈α
LISP.␈α
 This␈α
has␈α
many␈α
motivations:
␈↓␈↓↓128  symbol tables␈↓ )6.17␈↓


␈↓␈↓↓1.␈↓␈α∃To␈α∃describe␈α∃the␈α∃compiler␈α∃we␈α∃must␈α∃carefully␈α∃define␈α∃the␈α∃machine␈α∃which␈α∃will␈α∃execute␈α∃the
␈↓instructions␈α∂which␈α∂the␈α∂compile␈α∂produces.␈α∂ Examination␈α∂of␈α∂a␈α∂machine␈α∂will␈α∂reinforce␈α∂the␈α∂ideas␈α∂of
␈↓stacks,␈α∞make␈α
more␈α∞concrete␈α
the␈α∞structure␈α
of␈α∞recursion,␈α
of␈α∞garbage␈α
collection␈α∞and␈α∞representation␈α
of
␈↓sexprs␈α
and␈α
list␈α
structure.

␈↓␈↓↓2.␈↓␈α
It␈α
will␈α
show␈α
a␈α
very␈α
non-trivial␈α
application␈α
of␈α
non-numerical␈α
computation.␈α
At␈α
the␈α
same␈α
time␈αwe
␈↓will␈α
see␈α
how␈α
simple␈α
it␈α
is␈α
to␈α
describe␈α
such␈α
complex␈α
algorithms␈α
in␈α
LISP.

␈↓␈↓↓3.␈↓␈α⊂It␈α∂will␈α⊂clearly␈α∂show␈α⊂the␈α∂relationship␈α⊂between␈α∂compilation␈α⊂and␈α∂evaluation.␈α⊂ That␈α∂is,␈α⊂the␈α∂LISP
␈↓function␈αrepresenting␈αthe␈αcompiler␈αwill␈αvery␈αclosely␈αparallel␈αthe␈αstructure␈αof␈αthe␈αevaluator,␈α
␈↓αeval␈↓.␈α If
␈↓you␈α
understand␈α
␈↓αeval␈↓,␈α
then␈α
the␈α
compiler␈α
is␈α
easy.

␈↓␈↓↓4.␈↓␈α
Everyone␈α
should␈α
see␈α
a␈α
compiler␈α
("␈↓↓Shut␈α
up!␈↓",␈α
he␈α
explained).



␈↓␈↓ ¬⊗␈↓↓6.18  ␈↓	SM␈↓↓:A Simple machine␈↓	


␈↓This␈α∪section␈α∩describes␈α∪a␈α∩simple␈α∪machine␈α∩which␈α∪has␈α∩a␈α∪sufficient␈α∩instruction␈α∪set␈α∩to␈α∪handle␈α∩an
␈↓implementation␈α∪of␈α∪LISP.␈α∪ Before␈α∪we␈α∪begin,␈α∪note␈α∪that␈α∪this␈α∪machine␈α∪is␈α∪␈↓↓not␈↓␈α∪necessary␈α∪for␈α∪our
␈↓understanding␈α∃of␈α⊗␈↓αeval␈↓.␈α∃␈↓αeval␈↓␈α⊗is␈α∃self-contained.␈α⊗We␈α∃need␈α⊗only␈α∃describe␈α⊗a␈α∃machine␈α⊗to␈α∃discuss
␈↓implementation␈α_and␈α_compilation.␈α→ This,␈α_indeed␈α_is␈α_an␈α→objection␈α_to␈α_describing␈α→meaning␈α_of
␈↓programming␈α⊂languages␈α⊂in␈α⊂terms␈α⊂of␈α⊂a␈α⊂compiler␈α⊂--␈α⊂you␈α⊂must␈α⊂understand␈α⊂␈↓↓two␈↓␈α⊂things␈α⊂now␈α⊃--␈α⊂the
␈↓language␈α
␈↓↓and␈↓␈α
a␈α
machine.

␈↓The␈αsimple␈αmachine,␈α␈↓	SM␈↓,␈αhas␈αa␈αslight␈αsimilarity␈αto␈αthe␈αorganization␈αof␈αthe␈αPDP-10.␈αWe␈αneed␈αvery
␈↓few␈α⊂features␈α⊂to␈α⊂adequately␈α⊂discuss␈α⊂the␈α⊂interesting␈α⊂facets␈α⊂of␈α⊂implementation␈α⊂of␈α⊂our␈α⊂LISP␈α⊂subset.
␈↓Certainly,␈α∪if␈α∪we␈α∪were␈α∪to␈α∪undertake␈α∪a␈α∪real␈α∪implementation,␈α∪many␈α∪more␈α∪instructions␈α∪would␈α∪be
␈↓necessary.␈α∂Similarly,␈α∞when␈α∂we␈α∞discuss␈α∂compilation␈α∂our␈α∞␈↓	SM␈↓␈α∂suffices,␈α∞but␈α∂if␈α∞we␈α∂wished␈α∂to␈α∞perform
␈↓␈↓↓efficient␈↓␈α⊃compilation␈α⊃we␈α⊃would␈α⊃hopefully␈α⊃have␈α⊃a␈α⊃better␈α⊃instruction␈α⊃set.␈α⊃ The␈α⊃point␈α⊃here␈α⊃is␈α⊃to
␈↓understand␈α∀basic␈α∃algorithms.␈α∀If␈α∃that␈α∀is␈α∀accomplished␈α∃it␈α∀is␈α∃quite␈α∀straightforward␈α∃to␈α∀examine
␈↓problems␈α
of␈α
efficiency,␈α
and␈α
details␈α
of␈α
implementation.

␈↓␈↓	SM␈↓␈α⊂has␈α⊂a␈α⊂conventional␈α⊂addressable␈α⊂main␈α⊂memory,␈α∂and␈α⊂n␈α⊂special␈α⊂registers,␈α⊂AC1,␈α⊂AC2,␈α⊂...,␈α∂ACn.
␈↓These␈α∞registers␈α
are␈α∞called␈α
␈↓↓accumulators␈↓␈α∞and␈α
as␈α∞with␈α
the␈α∞AMBIT/G␈α
description,␈α∞these␈α∞n␈α
registers
␈↓are␈α∞to␈α∞contain␈α∞pointers␈α∞to␈α∂the␈α∞arguments␈α∞of␈α∞a␈α∞function␈α∂at␈α∞the␈α∞time␈α∞of␈α∞invocation.␈α∂ Each␈α∞memory
␈↓location␈αor␈αregister␈αis␈αassumed␈αto␈αbe␈αlarge␈αenough␈αto␈αcontain␈αtwo␈αaddresses.␈αFor␈αsake␈αof␈αdiscussion,
␈↓assume␈α⊂the␈α∂word␈α⊂size␈α⊂is␈α∂36␈α⊂bits.␈α⊂ Assume␈α∂the␈α⊂addressing␈α⊂space␈α∂is␈α⊂then␈α⊂2␈↓π18␈↓.␈α∂The␈α⊂mapping␈α⊂of␈α∂a
␈↓␈↓ε[~~~]~~~]␈↓␈αonto␈αa␈α␈↓	SM␈↓␈αlocation␈αis␈αeasy;␈αthe␈α␈↓αcar␈↓␈αmaps␈αto␈αthe␈αleft-half␈αof␈αthe␈αword;␈αthe␈α␈↓αcdr␈↓,␈αto␈αthe
␈↓right.␈α∩ A␈α∩memory␈α∪area␈α∩to␈α∩contain␈α∪full-words␈α∩(␈↓ε␈α∩[~~~~~~]␈↓␈α∩)␈α∪is␈α∩designated.␈α∩All␈α∪p-names␈α∩and
␈↓numbers␈α
are␈α
stored␈α
here.

␈↓Parts␈αof␈α␈↓	SM␈↓␈αmemory␈αcan␈αbe␈αdesignated␈αas␈αstacks.␈αEach␈αstack␈αis␈αa␈αcontiguous␈αarea␈αof␈αmemory,␈αand
␈↓the␈α⊃current␈α⊃top␈α⊂of␈α⊃the␈α⊃stack␈α⊃is␈α⊂referenced␈α⊃by␈α⊃a␈α⊂general␈α⊃register,␈α⊃P1,␈α⊃...,␈α⊂Pn,␈α⊃called␈α⊃a␈α⊃␈↓↓stack-␈↓␈α⊂or
␈↓␈↓↓6.18␈↓ λJ␈↓	SM␈↓↓:A Simple machine     129␈↓	


␈↓␈↓↓pushdown-␈↓␈α
pointer.␈α The␈α
stacks␈α
will␈αbe␈α
used␈αto␈α
contain␈α
the␈αpartial␈α
results␈αof␈α
calculations␈α
and␈αwill
␈↓contain␈α
the␈α
information␈α
necessary␈α
to␈αimplement␈α
recursive␈α
function␈α
calling.␈α
 The␈α
primary␈αLISP␈α
stack
␈↓is␈α
named␈α
P.

␈↓There␈α∩are␈α∩only␈α∪three␈α∩main␈α∩classes␈α∩of␈α∪instructions␈α∩necessary␈α∩to␈α∩describe␈α∪LISP␈α∩implementation:
␈↓instructions␈α
for␈αconstant␈α
generation,␈α
instructions␈αfor␈α
stack␈α
manipulation,␈αand␈α
instructions␈αfor␈α
control
␈↓of␈α
flow.

␈↓The␈α
control␈α
instructions␈α
and␈α
some␈αof␈α
the␈α
stack␈α
instructions␈α
refer␈αto␈α
the␈α
program␈α
counter␈α
of␈α␈↓	SM␈↓.␈α
␈↓	PC␈↓
␈↓designates␈α
this␈α
counter.␈α
␈↓αC␈↓␈α
in␈α
the␈α
following␈α
means␈α
"contents␈α
of...".

␈↓MOVEI ACi const␈↓ βh␈↓αC␈↓(ACi) ← const

␈↓PUSH P ACj␈↓ βh␈↓αC␈↓(␈↓αC␈↓(P)) ← ␈↓αC␈↓(ACj)
␈↓␈↓ βh␈↓αC␈↓(P) ← ␈↓αC␈↓(P)+1. Push contents of ACj onto top of stack.

␈↓POP P ACj␈↓ βh␈↓αC␈↓(P) ← ␈↓αC␈↓(P)-1
␈↓␈↓ βh␈↓αC␈↓(ACj) ← ␈↓αC␈↓(␈↓αC␈↓(P)). Pop top of stack into ACj.

␈↓POPJ P␈↓ βhP ← ␈↓αC␈↓(␈↓αC␈↓(P))-1
␈↓␈↓ βh␈↓αC␈↓(␈↓	PC␈↓) ← ␈↓αC␈↓(␈↓αC␈↓(P)). Pop top of stack into ␈↓	PC␈↓; used as return.


␈↓CALL i fn␈↓ βhThis␈α∪instruction␈α∩handles␈α∪function␈α∪calling.␈α∩i␈α∪is␈α∩the␈α∪number␈α∪of␈α∩arguments
␈↓␈↓ βx(assumed␈α∪to␈α∪be␈α∪in␈α∪AC1␈α∪through␈α∩ACi␈α∪at␈α∪time␈α∪of␈α∪call).␈α∪fn␈α∪represents␈α∩a
␈↓␈↓ βxfunction␈α
name.␈α One␈α
effect␈αof␈α
CALL␈αis␈α
to␈αleave␈α
return␈αinformation␈α
on␈αthe
␈↓␈↓ βxstack,␈α
P.

␈↓MOVE ACi -j P␈↓ βh␈↓αC␈↓(ACi) ← copy of the j␈↓πth␈↓ element (counting from zero␈↓πth␈↓) of stack P.

␈↓MOVEM ACi -j P␈↓ βhj␈↓πth␈↓ element of stack P is replaced by contents of ACi.


␈↓SUB x y␈↓ βh␈↓αC␈↓(x)␈α
←␈α
␈↓αC␈↓(x)␈α
-␈α
␈↓αC␈↓(y);␈α
Used␈α
in␈α
the␈α
context,␈α
SUB␈α
P␈α
const,␈α
to␈α
remove␈α
a␈α
block␈αof
␈↓␈↓ βxcells␈α
from␈α
the␈α
top␈α
of␈α
the␈α
stack.

␈↓JRST j␈↓ βh␈↓αC␈↓(␈↓	PC␈↓) ← j. Go to loc j.

␈↓JUMPE ACi j␈↓ βh␈↓↓if␈↓ ␈↓αC␈↓(ACi)=0 ␈↓↓then␈↓ ␈↓αC␈↓(␈↓	PC␈↓) ← j;

␈↓JUMPN ACi j␈↓ βh␈↓↓if␈↓ ␈↓αC␈↓(ACi)␈↓≠␈↓0 ␈↓↓then␈↓ ␈↓αC␈↓(␈↓	PC␈↓) ← j;


␈↓For␈α
much␈αof␈α
the␈αsequel,␈α
the␈αonly␈α
calling␈α
sequence␈αof␈α
interest␈αwill␈α
be␈αordinary␈α
function␈α
calls.␈α The
␈↓evaluated␈α∪arguments␈α∀are␈α∪to␈α∀be␈α∪loaded␈α∪into␈α∀the␈α∪accumulators.␈α∀ Internal␈α∪λ-expressions␈α∀are␈α∪not
␈↓␈↓↓130  symbol tables␈↓ (6.18␈↓


␈↓handled␈αyet.␈α(See␈αproblem␈α
IV␈αon␈αpage␈α154).␈α
 More␈αinstructions␈αfor␈α␈↓	SM␈↓␈α
will␈αbe␈αgiven␈αon␈α
page␈α153
␈↓when␈α
we␈α
discuss␈α
efficiency.



␈↓␈↓ ∧h␈↓↓6.19  An alternative: deep bindings␈↓


␈↓There␈α⊂are␈α⊂some␈α⊂weaknesses␈α⊃in␈α⊂the␈α⊂model␈α⊂of␈α⊃implementation␈α⊂espoused␈α⊂in␈α⊂the␈α⊃previous␈α⊂sections.
␈↓First,␈α∞as␈α∞we␈α∂have␈α∞seen␈α∞shallow␈α∞binding␈α∂requires␈α∞that␈α∞we␈α∞be␈α∂a␈α∞bit␈α∞careful␈α∞in␈α∂handling␈α∞functional
␈↓arguments.␈α
Also␈α
there␈α
is␈α
an␈α
appreciable␈α
overhead␈α
in␈α
changing␈α
contexts.

␈↓Second,␈α
our␈α∞strategy␈α
of␈α
requiring␈α∞argument␈α
passing␈α
in␈α∞a␈α
fixed␈α
number␈α∞of␈α
registers,␈α∞AC␈↓β1␈↓␈α
through
␈↓AC␈↓βn␈↓,␈α∞is␈α
unnecessarily␈α∞restrictive.␈α∞There␈α
will␈α∞be␈α∞some␈α
arbitrary␈α∞limit␈α∞on␈α
the␈α∞number␈α∞of␈α
arguments
␈↓which␈αa␈αfunction␈αmay␈α
have.␈αAfter␈αa␈αmoment's␈αreflection␈α
on␈αthe␈αrepresentation␈αof␈αthe␈α
symbol␈αtable
␈↓as␈α
a␈α
stack␈α
page␈α∞54,␈α
it␈α
should␈α
be␈α∞apparent␈α
that␈α
we␈α
might␈α
try␈α∞passing␈α
the␈α
arguments␈α
to␈α∞a␈α
function
␈↓requiring␈α␈↓αn␈↓␈αarguments␈αas␈αthe␈αfirst␈α␈↓αn␈↓␈αelements␈αof␈αa␈αstack.␈αThe␈αvalue␈αreturned␈αby␈αthe␈αfunction␈αcould
␈↓be␈α⊃defined␈α⊂to␈α⊃be␈α⊂the␈α⊃value␈α⊂located␈α⊃in␈α⊂the␈α⊃top␈α⊂of␈α⊃the␈α⊂stack.␈α⊃This␈α⊂holds␈α⊃promise.␈α⊃Consider␈α⊂the
␈↓evaluation␈α
of␈α
a␈α
form
␈↓α␈↓ ¬Ef[g␈↓β1␈↓α[ ... ]; ... g␈↓βn␈↓α[ ... ]]  .

␈↓The␈α∂evaluation␈α∂of␈α∂the␈α∂arguments␈α∂is␈α∂to␈α∂proceed␈α∂from␈α∂left␈α∂to␈α∂right.␈α∂If␈α∂we␈α∂perform␈α⊂the␈α∂procedure
␈↓"calculate␈α∂the␈α∂argument;␈α∂leave␈α∂value␈α∂in␈α∂top␈α∂of␈α∂stack",␈α∂then␈α∂␈↓αf␈↓␈α∂could␈α∂expect␈α∂to␈α∂find␈α∂values␈α∂for␈α∞its
␈↓λ-variables␈α
as:

␈↓␈↓ βxvalue stack:␈↓ ε8|  value for ␈↓αx␈↓βn␈↓␈↓ λx|
␈↓␈↓ βx␈↓ ε8|  value for ␈↓αx␈↓βn-1␈↓␈↓ λx|
␈↓␈↓ βx␈↓ ε8|       ...   ...␈↓ λx|
␈↓␈↓ βx␈↓ ε8|  value for ␈↓αx␈↓β1␈↓␈↓ λx|

␈↓There␈α
will␈αbe␈α
no␈α
problem␈αabout␈α
searching␈αto␈α
find␈α
the␈αvalues;␈α
␈↓αf␈↓␈α"knows"␈α
where␈α
to␈αfind␈α
the␈αvalues␈α
in
␈↓the␈α∂stack.␈α∞When␈α∂␈↓αf␈↓␈α∂is␈α∞finished␈α∂its␈α∂computation␈α∞it␈α∂need␈α∂only␈α∞remove␈α∂the␈α∂top␈α∞n␈α∂elements␈α∂from␈α∞the
␈↓value␈α
stack␈α
and␈α
add␈α
the␈α
value␈α
which␈α
it␈α
computed.␈α
 This␈α
scheme␈α
seems␈α
to␈α
have␈α
the␈α∞advantage␈α
of
␈↓shallow␈αbinding:␈αfast␈αaccess␈αto␈αvalues,␈αbut␈αnone␈αof␈αthe␈αdisadvantages.␈αIt␈αlooks␈αlike␈αthe␈αa-list␈αscheme
␈↓for␈α
binding␈α
and␈α
unbinding.␈α
What's␈α
the␈α
problem?␈α
It's␈α
global␈α
variables.

␈↓If␈α␈↓αf␈↓␈αwants␈αto␈αaccess␈αa␈αglobal␈αvariable␈αhow␈αcan␈αit␈αbe␈αfound?␈α All␈αwe␈αhave␈αstored␈αon␈αthe␈αstack␈αis␈αthe
␈↓␈↓↓value␈↓␈α∂and␈α⊂there␈α∂is␈α∂no␈α⊂way␈α∂that␈α⊂␈↓αf␈↓␈α∂can␈α∂"know"␈α⊂␈↓↓which␈↓␈α∂value␈α⊂is␈α∂the␈α∂value␈α⊂of␈α∂the␈α⊂global␈α∂variable.
␈↓Surely␈αwe␈αcan␈αsolve␈αthis␈αproblem:␈αsimply␈αstore␈α␈↓↓pairs␈↓  ---  name-value␈αpairs.␈α So␈αwhat's␈αthe␈αproblem
␈↓now?␈α∂The␈α∞expensive␈α∂calculation␈α∂only␈α∞arises␈α∂when␈α∞we␈α∂access␈α∂global␈α∞variables.␈α∂Most␈α∂variables␈α∞␈↓↓are␈↓
␈↓local␈αaren't␈α
they␈α---␈α
or␈αare␈α
they?␈αFunction␈αnames␈α
are␈αvariables␈α
and␈αare␈α
almost␈αalways␈α
global;␈α␈↓αcar␈↓␈αis␈α
a
␈↓variable␈α∞name,␈α∞whose␈α
"value"␈α∞is␈α∞a␈α
primitive␈α∞routine␈α∞to␈α
compute␈α∞the␈α∞␈↓αcar␈↓␈α
function.␈α∞Seldom␈α∞do␈α
you
␈↓wish␈α
to␈α
redefine␈α
␈↓αcar␈↓.

␈↓Searching␈αthe␈αstack␈α
for␈αname-value␈αpairs␈α
␈↓↓is␈↓␈αmore␈αexpensive␈α
than␈αpicking␈αup␈α
the␈αvalue␈αcell␈α
of␈αthe
␈↓␈↓↓6.19␈↓ πlAn alternative: deep bindings     131␈↓


␈↓shallow␈α∂binding␈α⊂scheme.␈α∂We␈α⊂will␈α∂soon␈α⊂see␈α∂that␈α⊂LISP␈α∂compilers␈α⊂for␈α∂deep␈α⊂binding␈α∂␈↓↓can␈↓␈α⊂be␈α∂made
␈↓efficient␈α
in␈α
their␈α
access␈α
of␈α
local␈α
and␈α
global␈αvariables;␈α
but␈α
the␈α
interpreter␈α
␈↓↓will␈↓␈α
have␈α
to␈α
search.␈αOne␈α
of
␈↓the␈α⊂worst␈α⊂cases␈α⊂will␈α⊂be␈α⊂the␈α⊂execution␈α⊂of␈α⊂a␈α⊂loop,␈α⊂in␈α⊂which␈α⊂accesses␈α⊂to␈α⊂the␈α⊂same␈α⊂variables␈α⊂occur
␈↓frequently.␈α This,␈αperhaps␈αis␈αanother␈αgood␈αreason␈αfor␈αremoving␈αcontrol␈αof␈αiteration␈αfrom␈αthe␈α
hands
␈↓of␈αthe␈α
programmer.␈α The␈α
extent␈αof␈α
(or␈αeven␈α
the␈αpresence␈α
of)␈αa␈α
loop␈αwhich␈α
the␈αuser␈α
is␈αcontrolling␈α
by
␈↓tests␈α∂and␈α∂goto's␈α∂is␈α∂difficult␈α∂to␈α∂discover.␈α∞ If␈α∂a␈α∂loop␈α∂is␈α∂controlled␈α∂by␈α∂language␈α∂constructs␈α∞(WHILE,
␈↓REPEAT,␈α∂etc.)␈α∞then␈α∂the␈α∞interpreter␈α∂might␈α∂have␈α∞some␈α∂chance␈α∞of␈α∂improving␈α∞the␈α∂execution␈α∂of␈α∞the
␈↓loop.

␈↓As␈α
we␈α∞have␈α
previously␈α∞said,␈α
deep␈α∞binding␈α
is␈α∞very␈α
reminiscent␈α∞of␈α
the␈α∞a-list␈α
symbol␈α∞table␈α
structure
␈↓which␈α∞was␈α
first␈α∞a␈α
stack,␈α∞then␈α∞embellished␈α
to␈α∞become␈α
a␈α∞tree␈α
when␈α∞functional␈α∞arguments␈α
appeared.
␈↓But␈α
recall␈α
that␈α
the␈α∞a-list␈α
scheme␈α
had␈α
each␈α
name-value␈α∞pair␈α
chained␈α
to␈α
its␈α
successor.␈α∞Pointers␈α
into
␈↓the␈α∞table␈α
structure␈α∞(environment␈α∞pointers)␈α
entered␈α∞into␈α∞this␈α
chain␈α∞at␈α∞various␈α
placees␈α∞to␈α∞define␈α
an
␈↓environment.␈αWith␈α
the␈αa-list␈α
scheme,␈αwe␈α
were␈αable␈αto␈α
generate␈αa␈α
tree␈αstructure,␈α
but␈αdeep␈αbinding,␈α
so
␈↓far,␈α
still␈α
has␈α
a␈α
stack-like␈α
structure.␈α
 Something␈α
must␈α
be␈α
done.␈α
Let's␈α
look␈α
at␈α
the␈α
a-list␈α
␈↓αeval␈↓␈α
again.

␈↓First␈α
note␈α
that␈αthe␈α
environment␈α
pointers␈αwe␈α
generated␈α
for␈αhandling␈α
functional␈α
arguments␈αpoint␈α
into
␈↓the␈α∂symbol␈α∂tables␈α∂in␈α∂very␈α⊂specific␈α∂places;␈α∂they␈α∂␈↓↓always␈↓␈α∂point␈α⊂into␈α∂the␈α∂tables␈α∂at␈α∂the␈α⊂beginning␈α∂of
␈↓λ-bindings.␈α∂They␈α∂will␈α∂␈↓↓never␈↓␈α∂point␈α∞into␈α∂the␈α∂interior␈α∂of␈α∂a␈α∞block␈α∂of␈α∂bindings.␈α∂Thus␈α∂each␈α∂␈↓↓block␈↓␈α∞of
␈↓λ-bindings␈α
can␈α
go␈αonto␈α
a␈α
stack␈αin␈α
a␈α
contiguous␈α
fashion.␈α Now␈α
recall␈α
the␈αWeizenbaum␈α
environments:
␈↓each␈αenvironment␈αhas␈αa␈αlocal␈αsymbol␈αtable␈α(λ-variables,␈αand␈α␈↓αprog␈↓-variables);␈αeach␈αenvironment␈αalso
␈↓has␈αentries␈αfor␈α
E␈↓βa␈↓␈αand␈αE␈↓βc␈↓.␈α
So␈αwe␈αmay␈αsimulate␈α
a␈αtree␈αon␈α
the␈αname-value␈αstack␈α
if␈αwe␈αinclude␈αin␈α
each
␈↓block␈α
of␈α
bindings,␈αpointers␈α
representing␈α
E␈↓βc␈↓␈αand␈α
E␈↓βa␈↓.␈α
Thus␈αthe␈α
search␈α
strategy␈αbecomes␈α
yet␈α
a␈αbit␈α
more
␈↓complex:␈α
when␈α∞seeking␈α
a␈α∞value␈α
for␈α∞a␈α
variable␈α
run␈α∞down␈α
the␈α∞name-value␈α
stack␈α∞comparing␈α
names;
␈↓when␈α
the␈α
block␈α
is␈α
exhausted␈α
we␈α
follow␈α
the␈α
E␈↓βa␈↓-pointer.␈α
 To␈α
exit␈α
a␈α
block␈α
is␈α
easy␈α
and␈α
in␈α∞fact␈α
much
␈↓easier␈α
than␈α
the␈α
shallow␈α
binding␈α
ritual:␈α
simply␈α
restore␈α
the␈α
E␈↓βc␈↓-pointer.

␈↓***more,more***
␈↓␈↓↓132  Compilation␈↓ C7.␈↓


␈↓␈↓ ¬␈␈↓↓SECTION 7

␈↓↓␈↓ ¬←COMPILATION␈↓






␈↓␈↓ ¬∩␈↓↓7.1  On LISP and Semantics␈↓


␈↓LISP␈α∩should␈α∩be␈α∩the␈α∩first␈α∩language␈α∩learned␈α∩by␈α∩Computer␈α∩Science␈α∩majors.␈α∩ As␈α∩a␈α∩mathematical
␈↓language␈αfor␈αstudies␈αalgorithms␈αfor␈αdata␈αstructures,␈αit␈αis␈αpresently␈αwithout␈αpeer.␈α As␈αyou␈αare␈αseeing
␈↓now,␈α
the␈α
problems␈α∞of␈α
language␈α
implementation␈α∞and␈α
their␈α
solutions␈α∞are␈α
describable␈α
quite␈α∞easily␈α
in
␈↓the␈α∂implementation␈α∂of␈α∞LISP␈α∂(symbol␈α∂tables,␈α∂hashing,␈α∞garbage␈α∂collection,␈α∂stacks,␈α∂linked␈α∞allocation,
␈↓lists,␈α∩etc.␈α∩etc.)␈α∪At␈α∩the␈α∩theoretical␈α∪level,␈α∩questions␈α∩of␈α∩provability␈α∪of␈α∩properties␈α∩of␈α∪programs␈α∩are
␈↓tractable.␈α As␈αa␈αprogramming␈αlanguage,␈αLISP␈αhas␈αexceptionally␈αpowerful␈αfeatures␈αpossessed␈αby␈αfew
␈↓languages.␈α⊂ In␈α⊃particular␈α⊂the␈α⊃uniform␈α⊂representation␈α⊃of␈α⊂program␈α⊃and␈α⊂data.␈α⊃ LISP␈α⊂is␈α⊃also␈α⊂quite
␈↓useful␈α∩in␈α∩understanding␈α∩the␈α∩semantics␈α∩of␈α⊃programming␈α∩languages.␈α∩ The␈α∩study␈α∩of␈α∩semantics␈α⊃is
␈↓motivated␈α
by␈αthe␈α
desire␈αto␈α
have␈α
a␈αtool␈α
for␈αdescribing␈α
the␈α
meaning␈αof␈α
constructs␈αof␈α
a␈α
language;␈αin
␈↓particular,␈α
a␈α
tool␈α
of␈α
the␈α
power␈α
and␈α
clarity␈α
of␈α
BNF␈α
descriptions␈α
of␈α
the␈α
syntax␈α
of␈α
languages.

␈↓The␈α∀field␈α∃of␈α∀programming␈α∀language␈α∃semantics␈α∀is␈α∀full␈α∃of␈α∀bull␈α∀written␈α∃by␈α∀people␈α∃who␈α∀don't
␈↓understand␈α⊂the␈α⊂problem.␈α⊂Here␈α⊂is␈α⊂some␈α⊃more␈α⊂by␈α⊂another.␈α⊂ There␈α⊂are␈α⊂many␈α⊂different␈α⊃schools␈α⊂of
␈↓thought␈α∞concerning␈α∂appropriate␈α∞means␈α∂for␈α∞describing␈α∞semantics.␈α∂ One␈α∞thought␈α∂is␈α∞to␈α∂describe␈α∞the
␈↓meaning␈αof␈αa␈α
language␈αin␈αterms␈α
of␈αthe␈αprocess␈αof␈α
compilation.␈α That␈αis,␈α
the␈αsemantic␈αis␈αspecified␈α
by
␈↓some␈α⊂canonical␈α⊂compiler␈α⊂producing␈α⊂code␈α⊂of␈α⊂some␈α⊂standard␈α⊂machine-independent␈α⊂machine.␈α∂ The
␈↓meaning␈α
of␈α
a␈α
program␈α
is␈α
the␈α
outcome␈α
of␈α
an␈α
interpreter␈α
interpreting␈α
this␈α
machine-independent␈α
code.

␈↓The␈αkey␈αproblem␈αis:␈αjust␈αwhat␈αis␈αthe␈αsemantic␈αspecification␈αsupposed␈αto␈αdo?␈α If␈αit␈αis␈αtruly␈αto␈αhelp␈αa
␈↓programmer␈α∪(be␈α∪he␈α∪implementor␈α∪or␈α∀applications)␈α∪to␈α∪understand␈α∪the␈α∪meaning␈α∪of␈α∀a␈α∪particular
␈↓constructs,␈αthen␈αthis␈α
proposal␈αis␈αlacking.␈α
 To␈αunderstand␈αa␈α
construct␈αnow␈αrequires␈α
that␈αyou␈αread␈α
the
␈↓description␈α⊗of␈α⊗a␈α⊗compiler--a␈α⊗non-trivial␈α↔task,␈α⊗and␈α⊗understand␈α⊗two␈α⊗machines--␈α↔the␈α⊗machine-
␈↓independent␈αand␈αthe␈αreal␈αmachine.␈α There␈αis␈αa␈αmore␈αfundamental␈αdifficulty␈αhere.␈α When␈αyou␈αlook
␈↓at␈α
a␈αstatement␈α
of␈α
a␈αhigh-level␈α
language␈α
you␈αthink␈α
about␈α
the␈αeffect␈α
the␈α
statement␈αwill␈α
have␈α
on␈αthe
␈↓environment␈α
of␈α
your␈α
program␈α
when␈α
it␈α
executes,␈α
you␈α
do␈α
not␈α
think␈α
about␈α
the␈α
code␈α
that␈α
gets␈α
generated
␈↓and␈αthen␈αthink␈αabout␈αthe␈αexecution␈αof␈αthat␈αcode.␈α Thus␈αmodeling␈αsemantics␈αin␈αterms␈αof␈αa␈αcompiler
␈↓model␈α∂is␈α∂addiing␈α∞one␈α∂more␈α∂level␈α∞of␈α∂obfuscation.␈α∂ A␈α∞more␈α∂natural␈α∂description␈α∞of␈α∂the␈α∂meaning␈α∞of
␈↓constructs␈αis␈α
given␈αin␈α
terms␈αof␈α
the␈αrun-time␈α
behavior␈αof␈α
these␈αconstructs.␈α
 That's␈αwhat␈α
LISP␈αdoes.
␈↓The␈α∩␈↓αeval␈↓␈α∩function␈α∩describes␈α⊃the␈α∩execution␈α∩sequence␈α∩of␈α⊃a␈α∩representation␈α∩of␈α∩an␈α∩arbitrary␈α⊃LISP
␈↓expression.␈α
 Thus␈α
␈↓αeval␈↓␈α
is␈α
the␈α
semantic␈α
description␈α
of␈α
LISP.␈α
 Now␈α
certain␈α
timid␈α
souls␈α∞shrink␈α
from
␈↓this␈α⊃resulting␈α⊃circularity:␈α⊃the␈α⊃description␈α⊃of␈α⊃the␈α⊃semantics␈α⊃of␈α⊃a␈α⊃language␈α⊃in␈α⊃that␈α⊃language,␈α⊃but
␈↓circularity,␈α
like␈α
death␈α
and␈α
taxes,␈α
is␈α
inevitable.
␈↓␈↓↓7.1␈↓ λ3On LISP and Semantics     133␈↓


␈↓Attempts␈α∩have␈α∪been␈α∩made␈α∩to␈α∪give␈α∩non-circular␈α∩interpreter-based␈α∪descriptions␈α∩of␈α∪semantics␈α∩for
␈↓languages␈αother␈αthan␈αLISP.␈α There␈αis␈αthe␈αincredible␈αVDL␈αdescription␈αof␈αPL/1;␈αand␈αthe␈αconvoluted
␈↓description␈α
of␈α
ALGOL␈α
68␈α
by␈α
a␈α
Markov␈α
algorithm.

␈↓To␈α∪describe␈α∪meaning␈α∪in␈α∪terms␈α∪of␈α∪'self-evident'␈α∪string␈α∪manipulations␈α∪is␈α∪misplaced␈α∪rigor.␈α∪ If␈α∩a
␈↓semantic␈αdescription␈αis␈αto␈αbe␈α
anything␈αmore␈αthan␈αa␈αsterile␈α
game␈αit␈αmust␈αbe␈αuseful␈α
to␈αimplementors
␈↓as␈α
well␈αas␈α
applications␈αprogrammers.␈α
 If␈αyou␈α
decide␈α
to␈αdescribe␈α
the␈αsemantics␈α
of␈αlanguage␈α
L␈↓β1␈↓␈α
in␈αa
␈↓simpler␈αlanguage␈αL␈↓β2␈↓␈αthen␈αeither␈αL␈↓β2␈↓␈αis␈α'self␈αevident'␈αor␈αyou␈αmust␈αgive␈αa␈αdescription␈αof␈αthe␈αmeaning
␈↓of␈α
L␈↓β2␈↓,␈α
etc.␈α
 So␈α
either␈α
you␈α
go␈α
circular␈α
or␈α
you␈α
end␈α
up␈α
with␈α
a␈α
(useless)␈α
'self-evident'␈α
L␈↓βn␈↓.

␈↓There␈α⊂are␈α∂very␈α⊂good␈α⊂reasons␈α∂for␈α⊂deciding␈α∂on␈α⊂direct␈α⊂circularity.␈α∂ First,␈α⊂you␈α∂need␈α⊂only␈α⊂learn␈α∂one
␈↓language.␈α↔ If␈α⊗the␈α↔semantic␈α↔specification␈α⊗is␈α↔given␈α⊗the␈α↔source␈α↔language,␈α⊗then␈α↔you␈α↔learn␈α⊗the
␈↓programming␈α⊃language␈α⊂and␈α⊃the␈α⊃semantic␈α⊂(meta)␈α⊃language␈α⊂at␈α⊃the␈α⊃same␈α⊂time.␈α⊃ Second,␈α⊃since␈α⊂the
␈↓evaluator␈α∪is␈α∪written␈α∩in␈α∪the␈α∪language,␈α∩we␈α∪can␈α∪understand␈α∩the␈α∪language␈α∪by␈α∪understanding␈α∩the
␈↓workings␈αof␈αthe␈αsingle␈αprogram,␈α␈↓αeval␈↓;␈αand␈αif␈αwe␈αwish␈αto␈αmodify␈αthe␈αsemantics␈αwe␈αneed␈αchange␈αonly
␈↓one␈α(source␈αlanguage)␈αprogram.␈α
 Thus,␈αif␈αwe␈αwished␈αto␈α
add␈αnew␈αlanguage␈αconstructs␈αto␈α
LISP␈α(like
␈↓the␈α∪␈↓αprog␈↓␈α∪feature)␈α∩we␈α∪need␈α∪only␈α∩modify␈α∪␈↓αeval␈↓␈α∪so␈α∩that␈α∪it␈α∪recognizes␈α∩an␈α∪occurrence␈α∪of␈α∪a␈α∩(sexpr
␈↓representation␈α⊃of␈α⊃a)␈α⊃␈↓αprog␈↓,␈α⊃add␈α⊃a␈α∩new␈α⊃(semantic)␈α⊃function␈α⊃to␈α⊃describe␈α⊃the␈α⊃interpretation␈α∩of␈α⊃the
␈↓construct␈α
and␈α
we're␈α
done.

␈↓A␈α⊂semantic␈α⊂description␈α⊂should␈α⊂not␈α⊂attempt␈α⊂to␈α⊂explain␈α⊂everything␈α⊂about␈α⊂a␈α⊂language.␈α⊃ You␈α⊂must
␈↓assume␈α∞that␈α∞your␈α∂reader␈α∞understands␈α∞something␈α∂...␈α∞.␈α∞McCarthy:␈α∞␈↓↓`Nothing␈α∂can␈α∞be␈α∞explained␈α∂to␈α∞a
␈↓↓stone'␈↓.

␈↓A␈αsemantic␈αdescription␈αof␈αa␈αlanguage␈αmust␈αbe␈αnatural.␈α It␈αmust␈αmatch␈αthe␈αexpressive␈αpower␈αof␈αthe
␈↓language.␈αA␈αsemantic␈αdescription␈αshould␈αalso␈αmodel␈αhow␈αthe␈αconstructs␈αare␈αto␈αbe␈αimplemented␈αon␈αa
␈↓reasonable␈α⊃machine.␈α⊃ What␈α⊃is␈α⊃needed␈α⊃is␈α⊃a␈α⊃semantic␈α⊃representation␈α⊃which␈α⊃exploits,␈α∩rather␈α⊃than
␈↓ignores,␈α
the␈α
structure␈α
of␈α
the␈α
language.␈α
 It␈α
can␈α
assume␈α
a␈α
certain␈α
level␈α
of␈α
understanding␈α
on␈α
the␈α
part␈α
of
␈↓the␈α
reader.␈α
 It␈α
need␈α
only␈α
explain␈α
what␈α
needs␈α
explaining.

␈↓Let's␈α
look␈α
at␈α∞syntax␈α
for␈α
a␈α∞moment.␈α
 A␈α
satisfactory␈α
description␈α∞of␈α
much␈α
of␈α∞programming␈α
language
␈↓syntax␈αis␈αstandard␈αBNF.␈α The␈αBNF␈αis␈αa␈αgenerative,␈αor␈αsynthetic␈αgrammar.␈α That␈αis,␈αrewriting␈αrules
␈↓specifying␈α∞how␈α∞to␈α∂generate␈α∞well␈α∞formed␈α∂strings␈α∞are␈α∞given.␈α∞ A␈α∂semantic␈α∞specification␈α∞on␈α∂the␈α∞other
␈↓hand␈αcan␈αbe␈αconsidered␈αto␈αbe␈αa␈αfunction␈αwhich␈αtakes␈αas␈αinput␈αan␈αarbitrary␈αprograms␈αand␈αgives␈αas
␈↓output␈α∂a␈α∂representation␈α∂of␈α∂the␈α∂value␈α∂of␈α∞executing␈α∂the␈α∂program.␈α∂ This␈α∂implies␈α∂that␈α∂our␈α∞semantic
␈↓function␈α
must␈α
have␈α
some␈α
way␈α
of␈α
analyzing␈α
the␈α
structure␈α
of␈α
the␈α
program.

␈↓In␈α
1962,␈αJohn␈α
McCarthy␈α
described␈αthe␈α
concept␈αof␈α
abstract␈α
analytic␈αsyntax.␈α
It␈α
is␈αanalytic␈α
in␈αthe␈α
sense
␈↓that␈α
it␈α
tells␈α
how␈α
to␈α
take␈α
programs␈α
apart..␈α
how␈α
to␈α
recognize␈α
and␈α
select␈α
subparts␈α
of␈α∞programs␈α
using
␈↓predicates␈α∞and␈α∞selector␈α∞functions.␈α∞ It␈α
is␈α∞abstract␈α∞in␈α∞the␈α∞sense␈α
that␈α∞it␈α∞makes␈α∞no␈α∞commitment␈α∞to␈α
the
␈↓external␈αrepresentation␈αof␈α
the␈αconstitutients␈αof␈αthe␈α
program.␈α We␈αneed␈αonly␈α
be␈αable␈αto␈αrecognize␈α
the
␈↓occurrence␈α
of␈α
a␈α
desired␈α
construct.␈α
For␈α
example:

␈↓␈↓ αlisterm[t]␈↓≡␈↓isvar[t]␈↓∨␈↓isconst[t]␈↓∨␈↓(issum[t]∧isterm[addend[t]]∧isterm[augend[t]])

␈↓and␈α
the␈α
BNF␈α
equation:
␈↓␈↓↓134  Compilation␈↓ 77.1␈↓


␈↓␈↓ ∧-<term> ::= <var> | <const> | <term> + <term>. 

␈↓issum,␈α
addend,␈α
and␈α
augend,␈α
don't␈α
really␈α
care␈α
whether␈α
the␈α
sum␈α
is␈α
represented␈α
as:

␈↓x+y,␈α∂or␈α∂+[x;y]␈α∂or␈α∂␈↓α(PLUS␈α∂X␈α∂Y)␈↓␈α∂or␈α∂xy+␈α∂.␈α∂The␈α∂different␈α∂external␈α∂representations␈α∂are␈α∂reflections␈α∂of
␈↓different␈α
concrete␈α
syntaxes.␈α
The␈α
above␈α
BNF␈α
equations␈α
give␈α
one.

␈↓What␈αis␈αgained?␈α Since␈αit␈αis␈αreasonable␈αto␈αassume␈αthat␈αthe␈αsemantics␈αis␈αto␈αoperate␈αon␈αsome␈αinternal
␈↓representation␈α∞of␈α
the␈α∞source␈α
program,␈α∞and␈α∞in␈α
fact,␈α∞a␈α
representation␈α∞reflecting␈α
the␈α∞structure␈α∞of␈α
the
␈↓program␈α
(commonly␈αknown␈α
as␈α
a␈αparse␈α
tree),␈α
we␈αcan␈α
describe␈α
the␈αsemantics␈α
of␈α
a␈αprogram␈α
in␈αterms␈α
of
␈↓a␈αfunction␈αwhich␈αoperates␈αon␈αa␈αparse␈αtree␈αusing␈αthe␈αpredicates␈αand␈αselectors␈αof␈αthe␈αanalytic␈αsyntax.
␈↓Abstract␈αsyntax␈αconcerns␈αitself␈αonly␈αwith␈αthose␈αproperties␈αof␈αa␈αprogram␈αwhich␈αare␈αof␈αinterest␈αto␈αan
␈↓evaluator.

␈↓You␈α∞may␈α
then␈α∞profitably␈α
think␈α∞of␈α
the␈α∞Meta␈α
expression␈α∞form␈α
of␈α∞LISP␈α
as␈α∞a␈α
concrete␈α∞syntax.␈α
 You
␈↓may␈α↔think␈α_of␈α↔the␈α↔M-␈α_to␈α↔S-expression␈α↔translator␈α_as␈α↔the␈α↔parser␈α_which␈α↔maps␈α_the␈α↔external
␈↓representation␈α∂onto␈α∂a␈α∂parse␈α∂(or␈α∂computational)␈α∂tree.␈α∂The␈α∂selectors␈α∂and␈α∂predicates␈α∂of␈α∂the␈α∂analytic
␈↓syntax␈α
are␈α
straightforward;␈α
recall␈α
the␈α
BNF␈α
for␈α
LISP:

␈↓<form>␈↓ αh::= <constant>
␈↓␈↓ αh::= <variable>
␈↓␈↓ αh::=<function>[<arg>; ... <arg>]
␈↓␈↓ αh::= [<form> → <form> ... <form> → <form>]
␈↓␈↓ αh  ....


␈↓Among␈α∂other␈α∂things,␈α∂then,␈α∂we␈α⊂need␈α∂to␈α∂recognize␈α∂constants␈α∂(␈↓αisconst␈↓),␈α∂variables␈α⊂(␈↓αisvar␈↓),␈α∂conditional
␈↓expressions␈α⊃(␈↓αiscond␈↓),␈α∩and␈α⊃functions␈α⊃(␈↓αisfun␈↓).␈α∩ We␈α⊃would␈α⊃then␈α∩need␈α⊃to␈α⊃write␈α∩a␈α⊃function␈α∩in␈α⊃some
␈↓language␈α
expressing␈α
the␈α
values␈α
of␈α
these␈α
constructs.␈α
Since␈α
the␈α
proposed␈α
evaluator␈α
is␈α∞to␈α
manipulate
␈↓parse␈αtrees,␈α
and␈αsince␈α
the␈αdomain␈αof␈α
LISP␈αfunctions␈α
␈↓↓is␈↓␈αbinary␈αtrees,␈α
it␈αshould␈α
seem␈αnatural␈α
to␈αuse
␈↓LISP␈α
itself.␈α
 If␈αthis␈α
is␈α
the␈αcase,␈α
then␈α
we␈αmust␈α
represent␈α
the␈αparse␈α
tree␈α
as␈αa␈α
LISP␈α
sexpr␈αand␈α
represent
␈↓the␈α
selectors␈α
and␈α
recognizers␈α
as␈α
LISP␈α
functions␈α
and␈α
predicates.␈α
 Perhaps:

␈↓αisconst[x]␈↓ αh:numberp[x]␈↓∨␈↓αeq[x;NIL]␈↓∨␈↓αeq[x;T]␈↓∨␈↓αeq[car[x];QUOTE]

␈↓αisvar[x]␈↓ αh:atom[x]␈↓∧¬␈↓α(eq[x;T]␈↓∨␈↓αeq[x;NIL]␈↓∨␈↓αnumberp[x])

␈↓αiscond[x]␈↓ αh:eq[car[x];COND]


␈↓So␈αto␈αrecapitulate,␈α
a␈αconcrete␈αsyntax␈αfor␈α
LISP␈αcan␈αbe␈αconceived␈α
of␈αas␈αthe␈αnormal␈α
Mexpr␈αnotation;
␈↓and␈α∩abstract␈α∩syntax␈α⊃for␈α∩LISP␈α∩can␈α∩be␈α⊃formulated␈α∩in␈α∩terms␈α∩of␈α⊃the␈α∩representation␈α∩of␈α∩the␈α⊃LISP
␈↓program␈α
as␈α
a␈α
Sexpr.␈α
 The␈α
the␈α
description␈α
of␈α
the␈α
semantics␈α
of␈α
LISP␈α
is␈α
simply␈α
␈↓αeval␈↓.

␈↓There␈α⊂are␈α⊂really␈α⊂two␈α⊂issues␈α⊂here:␈α⊂␈↓↓a␈↓␈α⊂representation␈α⊂of␈α⊂the␈α⊂analytic␈α⊂syntax␈α⊂of␈α⊂a␈α⊂language,␈α⊃and␈α⊂a
␈↓␈↓↓7.1␈↓ λ3On LISP and Semantics     135␈↓


␈↓representation␈α
in␈α
terms␈αof␈α
the␈α
language␈α
itself.␈α In␈α
conjunction,␈α
these␈α
two␈αideas␈α
give␈α
a␈α
natural␈αand
␈↓very␈α
powerful␈α
means␈α
of␈α
specifying␈α
semantics.

␈↓If␈α
this␈α
means␈α
of␈α
semantic␈α
specification␈α
␈↓↓is␈↓␈α
really␈α
all␈α
that␈α
good␈α
(does␈α
all␈α
good␈α
thing,␈α
no␈α
bad␈α
thing,␈α
and
␈↓takes␈α∂no␈α∂time)␈α∂then␈α∂we␈α∂should␈α∂be␈α∂able␈α∞to␈α∂specify␈α∂other␈α∂languages␈α∂similarly.␈α∂What␈α∂are␈α∂the␈α∞weak
␈↓points␈α~of␈α~LISP␈α~as␈α~`real'␈α~programming␈α→language?␈α~ Mainly␈α~the␈α~insistance␈α~on␈α~binary␈α→tree
␈↓representations␈α⊃of␈α⊃data.␈α⊃ It␈α⊃is␈α⊃quite␈α⊃clear␈α⊃that␈α⊃many␈α⊃applications␈α⊃could␈α⊃profit␈α⊃from␈α⊃other␈α⊃data
␈↓representations.␈α∂ What␈α∂we␈α∂would␈α⊂then␈α∂like␈α∂is␈α∂a␈α∂language␈α⊂which␈α∂has␈α∂richer␈α∂data␈α⊂structures␈α∂than
␈↓LISP␈αbut␈αwhich␈αis␈αdesigned␈αto␈αallow␈αLISP-style␈αsemantic␈αspecification.␈α That␈αis,␈αwe␈αwill␈αbe␈αable␈αto
␈↓give␈α∞an␈α∂analytic␈α∞syntactic␈α∞specification␈α∂for␈α∞the␈α∞language.␈α∂ We␈α∞will␈α∞be␈α∂able␈α∞to␈α∞write␈α∂an␈α∞evaluator,
␈↓albeit␈α↔more␈α↔complex␈α↔than␈α↔␈↓αeval␈↓,␈α↔in␈α↔the␈α⊗language␈α↔itself.␈α↔ The␈α↔evaluator␈α↔will␈α↔operate␈α↔on␈α⊗a
␈↓representation␈αof␈αthe␈α
program␈αas␈αa␈α
legal␈αdata␈αstructure␈αof␈α
the␈αlanguage,␈αjust␈α
as␈α␈↓αeval␈↓␈αoperates␈αon␈α
the
␈↓sexpr␈α∂translation␈α∂of␈α∂the␈α∂LISP␈α∞program.␈α∂ The␈α∂concrete␈α∂syntax␈α∂will␈α∞be␈α∂specified␈α∂as␈α∂a␈α∂set␈α∂of␈α∞BNF
␈↓equations,␈α
and␈α∞our␈α
parser␈α∞will␈α
translate␈α
legal␈α∞strings␈α
--␈α∞programs--␈α
into␈α
parse␈α∞trees.␈α
 In␈α∞outline,␈α
to
␈↓completely␈α
specify␈α
a␈α
construct␈α
in␈α
LISP␈α
we␈α
must␈α
have␈α
the␈α
following:

␈↓␈↓ ¬_␈↓↓1.␈↓  A concrete production
␈↓  **1**␈↓ ¬_␈↓↓2.␈↓  An abstract data type
␈↓␈↓ ¬_␈↓↓3␈↓.  A mapping from ␈↓↓1␈↓ to ␈↓↓2␈↓.
␈↓␈↓ ¬_␈↓↓4.␈↓  An evaluator for the abstract type.


␈↓The␈α`real'␈α
programming␈αlanguage␈α
EL1,␈αExtensible␈α
Language␈α1,␈α
␈↓↓has␈↓␈αin␈α
fact␈αbeen␈α
specified␈αin␈α
exactly
␈↓this␈αmanner.␈αWe␈αcould␈αnot␈αbegin␈αto␈αdescribe␈αthis␈αlanguage␈αhere;␈αrather␈αwe␈αwill␈αsketch␈αonly␈αa␈αbare
␈↓outline␈α∞of␈α∞the␈α∞ideas␈α∞involved.␈α∞As␈α∞with␈α∞LISP,␈α∞EL1␈α∞maps␈α∞programs␈α∞onto␈α∞data␈α∞structures.␈α∂EL1␈α∞has
␈↓richer␈α
data␈α
types␈α
including␈α
integers,␈α
characters,␈α∞pointers,␈α
and␈α
structures.␈α
Its␈α
syntax␈α
is␈α∞described␈α
in
␈↓BNF␈α∪and␈α∩a␈α∪mapping␈α∪from␈α∩well-formed␈α∪syntactic␈α∪units␈α∩to␈α∪data␈α∪structures␈α∩is␈α∪given.␈α∪The␈α∩EL1
␈↓evaluator␈αis␈αwritten␈αin␈αEL1␈αand␈αmanipulates␈αthe␈αdata-structure␈αrepresentation␈αof␈αEL1␈αprograms␈αin
␈↓a␈α
manner␈α
totally␈α
analogous␈α
to␈α
the␈α
LISP␈α
␈↓αeval␈↓␈α
function.

␈↓As␈αthe␈α
name␈αimplies,␈αa␈α
rationale␈αfor␈αEL1␈α
was␈αextensibility.␈αAn␈α
extensible␈αlanguage␈αis␈α
supposed␈αto
␈↓supply␈α∞a␈α∞base␈α∞or␈α∞core␈α∞language,␈α∞which␈α∞has␈α∞sufficient␈α∞handles␈α∞such␈α∞that␈α∞a␈α∞user␈α∞can␈α∂describe␈α∞new
␈↓data␈α∂structures,␈α⊂new␈α∂operations,␈α∂and␈α⊂new␈α∂control␈α⊂structures.␈α∂These␈α∂new␈α⊂objects␈α∂are␈α⊂described␈α∂in
␈↓terms␈αof␈α
combinations␈αof␈αconstructs␈α
in␈αthe␈αbase␈α
language.␈αExtensibility␈αis␈α
implicitly␈αcommited␈αto␈α
the
␈↓premiss␈α
that␈αthe␈α
power␈α
of␈αhigh␈α
level␈αlanguages␈α
is␈α
primarily␈αnotational␈α
rather␈α
than␈αcomputational.
␈↓An␈α
alternative␈α
to␈α
extensibility,␈α
called␈α
shell␈α
languages,␈α
is␈α
perhaps␈α
best␈α
exemplified␈α
by␈α
PL/1.

␈↓`Perhaps␈αthe␈α
most␈αimmediately␈α
striking␈αattribute␈αof␈α
PL/1␈αis␈α
its␈αbulk'.␈α"bulk"...␈α
 that's␈αa␈α
polite␈αword
␈↓for␈α∀another␈α∀four-letter␈α∀expletive.␈α∀ If␈α∀nothing␈α∀else␈α∀PL/1␈α∀should␈α∀have␈α∀the␈α∀beneficial␈α∀effect␈α∪of
␈↓illustrating␈α⊃the␈α⊂absurdity␈α⊃of␈α⊃languages␈α⊂which␈α⊃attempt␈α⊃to␈α⊂do␈α⊃all␈α⊂things␈α⊃for␈α⊃all␈α⊂people.␈α⊃??␈α⊃PL␈α⊂...
␈↓political␈α_language??␈α_The␈α↔sheer␈α_size␈α_of␈α↔such␈α_languages␈α_bodes␈α↔ill␈α_for␈α_efficient␈α↔compilation,
␈↓maintanence,␈αand␈αlearnability.␈α PL/1␈αalso␈αsuffers␈αfor␈αthe␈α"committee␈αeffect"␈αin␈αlanguage␈αdesign.␈αNo
␈↓great␈α⊗work␈α⊗of␈α↔art␈α⊗has␈α⊗ever␈α⊗been␈α↔designed␈α⊗"by␈α⊗committee";␈α⊗there␈α↔is␈α⊗no␈α⊗reason␈α↔to␈α⊗believe
␈↓programming␈α
language␈α
design␈α
should␈α
be␈α
any␈α
different.
␈↓␈↓↓136  Compilation␈↓ 77.1␈↓


␈↓We␈α∩have␈α∩frequently␈α∩seen␈α∩how␈α∩easy␈α∩it␈α∩has␈α∩been␈α∩to␈α∩extend␈α∩LISP␈α∩by␈α∩modifying␈α∩␈↓αeval␈↓.␈α∩ This␈α∩is
␈↓particularly␈α
easy␈α
because␈α
we␈αare␈α
making␈α
modifications␈α
at␈αthe␈α
level␈α
of␈α
data-structure␈αrepresntation␈α
of
␈↓programs.␈α⊂In␈α∂EL1␈α⊂we␈α⊂wish␈α∂to␈α⊂make␈α⊂the␈α∂extensions␈α⊂at␈α∂the␈α⊂level␈α⊂of␈α∂concrete␈α⊂syntax,␈α⊂rather␈α∂than
␈↓abstract␈αsyntax␈α
as␈αLISP␈αdoes.␈α
We␈αcan␈αdo␈α
this␈αby␈αusing␈α
a␈αparser␈αwhich␈α
is␈αtable-driven,␈αoperating␈α
on
␈↓an␈α
modifable␈αset␈α
of␈α
productions.␈αThe␈α
parser␈α
must␈αbe␈α
capable␈α
of␈αrecognizing␈α
occurrences␈α
of␈αa␈α
set␈α␈↓↓1.␈α
-
␈↓↓4.␈↓␈α
of␈α
**1**␈α
above,␈α
adding␈α
␈↓↓1␈↓␈α
to␈α
its␈α
set␈α
of␈α
productions,␈α
using␈α
␈↓↓2␈↓␈α
and␈α
␈↓↓3␈↓␈α
to␈α
effect␈α
the␈α
translations,␈α
and
␈↓using␈α
␈↓↓4␈↓␈α
to␈α
effect␈α
the␈α
evaluation␈α
of␈α
an␈α
instance␈α
of␈α
␈↓↓1␈↓.␈α
 This␈α
in␈α
essence␈α
is␈α
what␈α
EL1␈α
does.



␈↓␈↓ ¬$␈↓↓7.2  More on Compilation␈↓


␈↓As␈α
we␈α
have␈α∞described␈α
in␈α
previous␈α∞notes␈α
the␈α
processes␈α
of␈α∞evaluation␈α
and␈α
compilation␈α∞are␈α
parallel:
␈↓the␈α∀evaluator␈α∀performs␈α∪the␈α∀evaluation;␈α∀the␈α∀compiler␈α∪emits␈α∀instructions␈α∀which␈α∀when␈α∪executed
␈↓produce␈α
the␈α
same␈α
computational␈α
effect␈α
as␈α
the␈α
evaluator.␈α
 Since␈α
the␈α
code␈α
produced␈α
by␈α
the␈α
compiler␈α
is
␈↓either␈α
in␈α∞machine␈α
language␈α
or␈α∞in␈α
a␈α
form␈α∞closer␈α
to␈α
the␈α∞machine␈α
than␈α
the␈α∞source␈α
program,␈α∞we␈α
can
␈↓execute␈αthe␈α
code␈αmuch␈α
faster.␈αA␈αspeed-up␈α
factor␈αof␈α
ten␈αis␈αnot␈α
uncommon.␈α Also␈α
in␈αLISP␈α
we␈αknow
␈↓that␈α⊃programs␈α⊃are␈α⊃stored␈α⊃as␈α⊃sexpressions.␈α⊃ Our␈α⊃compiled␈α⊃code␈α⊃will␈α⊃be␈α⊃machine␈α⊃language,␈α⊃thus
␈↓freeing␈α∞a␈α∞large␈α∞quantity␈α∞of␈α∞sexpression␈α∞storage.␈α∞This␈α∞will␈α∞usually␈α∞make␈α∞garbage␈α∞collection␈α∞be␈α
less
␈↓time␈α
consuming.␈α
 Why␈α
not␈α
compile␈α
all␈α
programs?␈αWe␈α
already␈α
have␈α
seen␈α
that␈α
we␈α
can␈α
␈↓αcons␈↓-up␈αnew
␈↓expressions␈α
to␈α
be␈α
evaluated␈α
while␈α
we␈α
are␈α
running.␈α
Still␈α
we␈α
can␈α
force␈α
even␈α
those␈αexpressions␈α
through
␈↓a␈α
compiler␈α
before␈α
execution.␈α
The␈α
answer,␈α
rather,␈α
is␈α
that␈α
for␈α
debugging␈α
and␈α
editing␈α
of␈α
programs␈αit␈α
is
␈↓extremely␈α⊃convenient␈α∩to␈α⊃have␈α∩a␈α⊃structured␈α⊃representation␈α∩of␈α⊃the␈α∩program␈α⊃(like␈α∩sexpression)␈α⊃in
␈↓memory.␈α
We␈α
shall␈α
say␈α
more␈α
about␈α
this␈α
later.

␈↓We␈αshall␈αexploit␈α
the␈αanalogy␈αbetween␈α
compilers␈αand␈αevaluators␈αwhen␈α
we␈αwrite␈αthe␈α
LISP␈αfunction,
␈↓␈↓αcompile␈↓,␈αwhich␈αwill␈αdescribe␈αthe␈αcompiler.␈α We␈αshalll␈αdo␈αthis␈αin␈αtwo␈αways.␈αFirst,␈αthe␈αstructure␈αof␈αthe
␈↓␈↓αcompile␈↓␈α⊃function␈α⊃and␈α⊃its␈α∩subfunctions␈α⊃will␈α⊃be␈α⊃written␈α⊃to␈α∩capitalize␈α⊃on␈α⊃the␈α⊃knowledge␈α∩we␈α⊃have
␈↓acquired␈α
from␈αwriting␈α
evaluators.␈α
Second␈αand␈α
more␈α
difficult,␈αwe␈α
will␈α
attempt␈αto␈α
abstract␈α
from␈αthe
␈↓specific␈α
compiler,␈α
the␈α
essence␈α
of␈α
LISP␈α
compilers␈α
without␈α
losing␈α
all␈α
of␈α
the␈α
details.␈α
At␈α
the␈α
most␈α
general
␈↓level␈αa␈αcompiler␈αsimply␈α
produces␈αcode␈αwhich␈αwhen␈αexecuted␈α
has␈αthe␈αsame␈αeffect␈αas␈α
evaluating␈αthe
␈↓original␈α∂form,␈α∂but␈α⊂this␈α∂description␈α∂has␈α∂lost␈α⊂too␈α∂much␈α∂detail.␈α∂ This␈α⊂second␈α∂task␈α∂is␈α⊂more␈α∂difficult
␈↓because␈α
we␈α
have␈α
to␈α
separate␈α
two␈α
representations␈α
from␈α
the␈α
specific␈α
compiler.␈α
We␈α
are␈αrrepresenting
␈↓forms␈α∂as␈α∞data␈α∂structures;␈α∞and␈α∂we␈α∞are␈α∂also␈α∞dealing␈α∂with␈α∞the␈α∂representation␈α∞of␈α∂a␈α∂specific␈α∞machine.
␈↓The␈α
task␈α
␈↓↓is␈↓␈α
worth␈α
persuing␈α
since␈α
we␈α
wish␈α
to␈α
write␈α
different␈α
compilers␈α
for␈α
the␈α
same␈α∞machine␈α
and
␈↓also␈α
a␈α
single␈α
compiler␈α
capable␈α
of␈α
easy␈α
transportation␈α
to␈α
other␈α
machines.

␈↓The␈α∂input␈α∞to␈α∂␈↓αcompile␈↓␈α∞is␈α∂the␈α∂sexpr␈α∞representation␈α∂of␈α∞a␈α∂LISP␈α∂function;␈α∞the␈α∂output␈α∞is␈α∂a␈α∂list␈α∞which
␈↓represents␈αa␈αsequence␈αof␈αmachine␈αinstructions.␈α Assume␈αthat␈αwe␈αhave␈αLISP␈αrunning␈αon␈α␈↓	Brand␈αX␈↓
␈↓machine,␈α
and␈αwe␈α
have␈αjust␈α
written␈α
the␈αLISP␈α
function,␈α␈↓αcompile␈↓,␈α
which␈αproduces␈α
code␈α
for␈α␈↓	Brand
␈↓	X␈↓␈α
machine.␈α
Then:
␈↓␈↓↓7.2␈↓ λXMore on Compilation     137␈↓



␈↓1.␈α
 Write␈α
the␈α
Sexpression␈α
form␈α
of␈α
␈↓αcompile␈↓.

␈↓2.␈α
 Read␈α
in␈α
this␈α
translation,␈α
defining␈α
␈↓αcompile␈↓.

␈↓3.␈α
 Ask␈α
LISP␈α
to␈α
evaluate:␈α
␈↓αcompile[COMPILE]␈↓.

␈↓Now␈α
since␈α
␈↓αcompile␈↓␈α
is␈α
a␈αfunction␈α
of␈α
one␈α
argument␈α
which␈αproports␈α
to␈α
compile␈α
code␈α
for␈α
␈↓	Brand␈αX␈↓
␈↓machine,␈αtranslating␈αthe␈αsexpression␈αrepresentation␈αof␈αits␈αargument␈αto␈αmachine␈αcode,␈αthe␈αoutput␈αof
␈↓step␈α3.␈αshould␈αbe␈αa␈αlist␈αof␈αinstructions␈αrepresenting␈αthe␈αtranslation␈αof␈αthe␈αfunction␈α␈↓αcompile␈↓.␈α That␈α
is,
␈↓step␈α
3.␈α
compiles␈α
the␈α
compiler!!

␈↓A␈αtechnique␈αcalled␈α␈↓↓bootstrapping␈↓␈αis␈αclosely␈αrelated␈αto␈αthe␈αprocess␈αdescribed␈αabove.␈αAssume␈αthat␈αwe
␈↓have␈αLISP␈αand␈αits␈αcompiler␈αrunning␈αon␈α␈↓	Brand␈αX␈↓,␈αand␈αwe␈αwish␈αto␈αimplement␈αLISP␈α(quickly)␈αon
␈↓␈↓	Brand␈αY␈↓.␈αIf␈αwe␈αhave␈αbeen␈αcareful␈αin␈αour␈αencoding␈αof␈αthe␈α␈↓αcompile␈↓␈αfunction␈αthen␈α␈↓↓most␈↓␈αof␈α␈↓αcompile␈↓
␈↓is␈αmachine␈αindependent,␈αthat␈αis␈αit␈αdeals␈αmostly␈αwith␈αthe␈αstructure␈αof␈αLISP␈αand␈αonly␈αin␈αa␈αfew␈αplaces
␈↓deals␈α∩with␈α∪the␈α∩structure␈α∪of␈α∩the␈α∪particular␈α∩machine.␈α∪ As␈α∩we␈α∪will␈α∩see␈α∪this␈α∩is␈α∪not␈α∩too␈α∪great␈α∩an
␈↓assumption␈α∃to␈α⊗make.␈α∃ Notice␈α⊗that␈α∃this␈α⊗is␈α∃one␈α∃of␈α⊗our␈α∃admonitions:␈α⊗encode␈α∃algorithms␈α⊗in␈α∃a
␈↓representation-independent␈α∞style␈α∞and␈α∞include␈α∞representation-dependent␈α∞rountines␈α∞to␈α∂interface␈α∞with
␈↓the␈αprogram.␈α
To␈αchange␈α
representations,␈αsimply␈α
requires␈αchanging␈α
those␈αsimpler␈αsubfunctions.␈α
Here
␈↓the␈α
repesentations␈α
are␈α
machines␈α
and␈α
the␈α
algorithm␈α
is␈α
a␈α
compiling␈α
function␈α
for␈α
LISP.

␈↓Let␈α
us␈αcall␈α
those␈α
parts␈αof␈α
the␈α
compiler␈αwhich␈α
deal␈α
with␈αthe␈α
machine,␈α
the␈αcode␈α
generators.␈α
Now␈αif␈α
we
␈↓understand␈αthe␈αmachine␈α
organization␈αof␈αbrands␈α␈↓	X␈↓␈α
and␈α␈↓	Y␈↓␈αthen␈αfor␈α
any␈αinstruction␈αon␈α
␈↓	Brand␈αX␈↓
␈↓we␈αshould␈αbe␈αable␈αto␈αgive␈αa␈α(sequence␈αof)␈αinstructions␈αhaving␈αthe␈αequivalent␈αeffect␈αon␈α␈↓	Brand␈αY␈↓.
␈↓In␈αthis␈αmanner␈αwe␈αcan␈αchange␈αthe␈αcode␈αgenerators␈αin␈α␈↓αcompile␈↓␈αto␈αgenerate␈αcode␈αto␈αrun␈αon␈α␈↓	Brand
␈↓	Y␈↓.␈αWe␈αthus␈αwould␈αhave␈αa␈α␈↓αcompile␈↓␈αfunction,␈αcall␈αit␈α␈↓αcompile*␈↓,␈αrunning␈αon␈α␈↓	X␈↓␈αand␈αproducing␈αcode␈αfor
␈↓␈↓	Y␈↓.␈α⊂ Now␈α⊂if␈α⊂we␈α⊂take␈α⊂the␈α⊂Sexpr␈α⊂forms␈α⊂of␈α⊂␈↓αeval,␈α⊂apply,␈α⊂read,␈α⊂print,␈α⊂compile,...␈↓␈α⊂etc.␈α⊂ and␈α⊃pass␈α⊂these
␈↓functions␈α∞through␈α∞␈↓αcompile*␈↓,␈α∞we␈α∂will␈α∞get␈α∞a␈α∞large␈α∂segment␈α∞of␈α∞a␈α∞LISP␈α∂system␈α∞which␈α∞will␈α∞run␈α∂on␈α∞␈↓	Y␈↓.
␈↓Certain␈α
primitives␈αwill␈α
have␈αto␈α
be␈αsupplied␈α
to␈αrun␈α
this␈αcode␈α
on␈α␈↓	Y␈↓,␈α
but␈αa␈α
very␈αlarge␈α
part␈α
of␈αLISP
␈↓can␈α
be␈α
bootstrapped␈α
from␈α
␈↓	X␈↓␈α
to␈α
␈↓	Y␈↓.

␈↓Obviously,␈αbefore␈αwe␈αcan␈αuse␈α␈↓↓a␈↓␈α
compiled␈αversion␈αof␈αthe␈αcompiler␈α(or␈α
in␈αfact,␈αbefore␈αwe␈αcan␈αuse␈α
any
␈↓compiled␈αcode)␈α
we␈αmust␈α
have␈αsome␈α
means␈αof␈αtranslating␈α
the␈αlist␈α
output␈αof␈α
the␈αcompile␈αfunction␈α
into
␈↓real␈α
instructions␈α
in␈α
the␈α
machine.␈α
 So␈α
if␈α
the␈α
version␈α
of␈α
LISP␈α
which␈α
we␈α
are␈α
implementing␈α
is␈α
to␈αhave␈α
a
␈↓compiler␈α
we␈α
must␈α
allocate␈α
an␈α
area␈α
of␈α
memory␈αwhich␈α
can␈α
receive␈α
the␈α
compiled␈α
code.␈α
 This␈α
area␈αis
␈↓usually␈α⊂called␈α⊂␈↓↓Binary␈α⊂Program␈α⊂Space␈↓␈α⊂(BPS).␈α⊂ The␈α⊂translation␈α⊂program␈α⊂which␈α⊂takes␈α⊂the␈α⊂list␈α⊂of
␈↓output␈α∞from␈α∂the␈α∞compiler␈α∂and␈α∞converts␈α∂it␈α∞onto␈α∂actual␈α∞machine␈α∂instructions␈α∞for␈α∂BPS␈α∞is␈α∂called␈α∞an
␈↓assembler.␈α→Before␈α_discussing␈α→assemblers␈α→we␈α_will␈α→examine␈α→a␈α_sequence␈α→of␈α→simple␈α_compilers
␈↓corresponding␈αto␈αthe␈αLISP␈αsubsets␈αevaluated␈αby␈α␈↓αtgmoaf␈↓,␈α␈↓αtgmoafr␈↓␈αand␈αfinally␈αan␈α␈↓αeval␈↓␈αwhich␈αhandles
␈↓local␈α
variables.
␈↓␈↓↓138  Compilation␈↓ 57.3␈↓


␈↓␈↓ ∧g␈↓↓7.3  Compilers for subsets of LISP␈↓


␈↓We␈α⊂will␈α⊂examine␈α∂compilers␈α⊂for␈α⊂increasingly␈α∂complex␈α⊂subsets␈α⊂of␈α∂LISP␈α⊂beginning␈α⊂with␈α∂functions,
␈↓composition␈α∞and␈α∞constant␈α∂arguments␈α∞and␈α∞ending␈α∂with␈α∞a␈α∞more␈α∂realistic␈α∞compiler␈α∞for␈α∂a␈α∞reasonable
␈↓subset␈α⊂of␈α⊂pure␈α∂LISP.␈α⊂Though␈α⊂each␈α⊂subset␈α∂is␈α⊂a␈α⊂simple␈α⊂extension␈α∂of␈α⊂its␈α⊂predecessor,␈α⊂each␈α∂subset
␈↓introduces␈αa␈αnew␈αproblem␈αto␈αbe␈αsolved␈αby␈αthe␈αcompiling␈αalgorithm.␈α If␈αthe␈αcorresponding␈αevaluator
␈↓(␈↓αtgmoaf␈↓,␈α␈↓αtgmoafr␈↓␈αand␈αthe␈αmost␈αsimple␈α␈↓αeval␈↓)␈αis␈αwell␈αunderstood,␈αthen␈αthe␈αnecessary␈αsteps␈αto␈αbe␈αadded
␈↓to␈α
the␈α
compiler␈α
are␈α
easy␈α
to␈α
make.

␈↓First␈αwe␈αneed␈αto␈αbegin␈αdescribing␈αa␈αlist␈α
of␈αconventions␈αwhich␈αwill␈αremain␈αin␈αeffect␈αthrough␈αout␈α
this
␈↓sequence␈α∃of␈α∃compilers.␈α∃ The␈α∃code␈α∃which␈α∀is␈α∃generated␈α∃must␈α∃be␈α∃compatible␈α∃with␈α∃that␈α∀which
␈↓incorporates␈αthe␈αbasic␈αLISP␈αmachine.␈α In␈αparticular␈αthe␈αcalling␈αconventions␈αfor␈αfunction␈αinvocation
␈↓must␈α
be␈α
maintained.␈α
 Thus␈α
a␈α
function␈α
of␈α␈↓αn␈↓␈α
arguments␈α
expects␈α
its␈α
arguments␈α
to␈α
be␈α
presented␈αin␈α
AC1
␈↓through␈α
ACn.␈α
Also␈α
the␈α
execution␈α
of␈α
any␈αcode␈α
which␈α
is␈α
computing␈α
arguments␈α
to␈α
a␈α
function␈αcall␈α
must
␈↓store␈α⊂temporary␈α⊂results␈α⊂on␈α⊂the␈α⊂stack,␈α⊂␈↓αP␈↓.␈α∂Thus␈α⊂the␈α⊂execution␈α⊂sequence␈α⊂of␈α⊂code␈α⊂for␈α⊂computing␈α∂␈↓αn␈↓
␈↓arguments␈α
should␈α
be:
␈↓␈↓ ∧∃compute value of argument␈↓β1␈↓, push onto stack, ␈↓αP␈↓.

␈↓␈↓ ε2..........
␈↓␈↓ ∧⊗compute value of argument␈↓βn␈↓, push onto stack, ␈↓αP␈↓.

␈↓So␈α∂after␈α∂this␈α∂computation␈α⊂the␈α∂values␈α∂of␈α∂the␈α⊂arguments␈α∂are␈α∂stored␈α∂V␈↓βn␈↓,␈α⊂V␈↓βn-1␈↓,␈α∂...,␈α∂V␈↓β1␈↓,␈α∂from␈α⊂top␈α∂to
␈↓bottom␈αin␈α␈↓αP␈↓.␈α Thus␈αto␈αcomplete␈αthe␈αfunction␈αinvocation,␈αwe␈αneed␈αonly␈αpop␈αthe␈αarguments␈αinto␈αthe
␈↓AC's␈α
in␈αto␈α
obvious␈αorder.␈α
This␈α
brings␈αus␈α
to␈αthe␈α
last␈α
(and␈αthe␈α
most␈αimportant!)␈α
convention␈α
for␈αthe
␈↓initial␈α
compiler.␈α
We␈α
must␈α
always␈α
be␈α
sure␈α
that␈αthe␈α
integrity␈α
of␈α
the␈α
stack␈α
is␈α
maintained.␈α
What␈αever␈α
we
␈↓push␈α∞onto␈α
the␈α∞stack␈α∞within␈α
the␈α∞body␈α
of␈α∞a␈α∞function␈α
␈↓¬must␈↓␈α∞be␈α
popped␈α∞off␈α∞before␈α
we␈α∞exit␈α∞from␈α
the
␈↓function␈αbody.␈αThat␈αis,␈αthe␈αstate␈αof␈αthe␈αstack␈αmust␈αbe␈αtransparent␈αto␈αany␈αcomputations␈αwhich␈αoccur
␈↓within␈αthe␈αfunction.␈α The␈αfirst␈αcompiler␈αwill␈αhandle␈α(the␈αS-expression␈αtranslation␈αof)␈αthat␈αsubset␈αof
␈↓LISP␈α
forms␈α
defined␈α
by␈α
the␈α
following␈α
BNF␈α
equations:

␈↓<form>␈↓ αh::= <constant> | <function>[<arg>; ...; <arg>] | <function> [ ]

␈↓<arg>␈↓ αh::= <form>

␈↓<constant>␈↓ αh::= <sexpr>

␈↓<function>␈↓ αh::= <identifier>


␈↓This␈αLISP␈αsubset␈αcorresponds␈αclosely␈αwith␈α␈↓αtgmoaf␈↓,␈αhandling␈αonly␈αfunction␈αnames,␈αcomposition,␈αand
␈↓constant␈αarguments.␈α
 Our␈αprevious␈α
compiling␈αconventions␈αwill␈α
keep␈αus␈α
in␈αgood␈α
stead.␈αWe␈αneed␈α
only
␈↓consider␈α
the␈α
compilation␈α
of␈α
constants.␈α
A␈α
S-expression␈α
constant␈α
will␈α
be␈α
seen␈α
as␈α
␈↓α(QUOTE␈α
...)␈↓␈α
by␈αthe
␈↓compiler.␈α
The␈α
code␈α
which␈α
we␈α
wish␈α
to␈α
generate␈α
is:

␈↓α␈↓ ¬_(MOVEI AC1 (QUOTE ...))
␈↓␈↓↓7.3␈↓ π←Compilers for subsets of LISP     139␈↓


␈↓It␈α
should␈α
now␈α
be␈α
clear␈α
how␈α
to␈α
proceed␈α
with␈α
the␈α
first␈α
compiler.

␈↓αcompexp <=␈↓ αhλ[[exp]␈↓ βX[eq[car[exp];QUOTE] → list[list[MOVEI;AC1;exp]];
␈↓α␈↓ αh␈↓ βX T → compapply[car[exp];complis[cdr[exp]];length[cdr[exp]]]] ]

␈↓αcomplis <=␈↓ αhλ[[u]␈↓ βX[null[u] → NIL;
␈↓α␈↓ αh␈↓ βX T → append[compexp[car[u]];((PUSH P AC1));complis[cdr[u]]]] ]

␈↓αcompapply <= λ[[fn;args;n]append[args;loadac[n];list[list[CALL;n;list[E;fn]]]]]

␈↓αloadac <=␈↓ αhλ[m]␈↓ βX[zerop[m] → NIL;
␈↓α␈↓ αh␈↓ βX T → cons[list[POP;P;cdr[assoc[n;aclist]]];loadac[n-1]] ]

␈↓α␈↓where:␈↓α aclist ␈↓is␈↓α ((1 . AC1) ... (N . ACn)).


␈↓Notice␈α
that␈α
the␈α
actual␈α
function␈α
call␈α
generated␈α
by␈α
␈↓αcompapply␈↓␈α
is␈α
of␈α
the␈α
form:

␈↓␈↓ ¬S␈↓α(CALL␈↓ n␈↓α(E␈↓ name␈↓α))␈↓ 

␈↓The␈α
␈↓αE␈↓-construct␈α
is␈α
discussed␈α
in␈α
the␈α
next␈α
section␈α
on␈α
assembling.

␈↓The␈α∂code␈α∂generate␈α∂by␈α∂this␈α∂compiler␈α∂is␈α∂clearly␈α⊂inefficient,␈α∂but␈α∂that␈α∂is␈α∂not␈α∂the␈α∂point.␈α∂We␈α⊂wish␈α∂to
␈↓establish␈α
a␈α
correct␈α
compiler,␈α
then␈α
later␈α
worry␈α
about␈α
efficiency.

␈↓The␈α∂innovation␈α⊂which␈α∂occurred␈α⊂in␈α∂␈↓αtgmoafr␈↓␈α∂was␈α⊂the␈α∂apprearance␈α⊂of␈α∂conditional␈α⊂expressions.␈α∂To
␈↓describe␈α
conditional␈α
expressions,␈α
the␈α
above␈α
BNF␈α
equations␈α
were␈α
augmented␈α
by:

␈↓␈↓ ∧α<form> ::= [<form> → <form> ; ... <form> → <form>] 

␈↓Certainly␈α
the␈α
addition␈α
of␈α
conditional␈α
expressions␈α
will␈α
mean␈α
an␈α
extra␈α
piece␈α
of␈α
code␈α
in␈α∞␈↓αcompexp␈↓␈α
to
␈↓recognize␈α␈↓αCOND␈↓␈αand␈αa␈αnew␈αfunction␈α(analogous␈αto␈α␈↓αevcond␈↓␈αin␈α␈↓αtgmoafr␈↓)␈αto␈αgenerate␈αthe␈αcode␈αfor␈αthe
␈↓␈↓αCOND␈↓-body.␈α In␈αfact,␈αthe␈αonly␈αdifference␈αbetween␈α␈↓αevcond␈↓␈αand␈αits␈αcounterpart␈αin␈α␈↓αcompexp␈↓,␈αwhich␈α
we
␈↓shall␈αcall␈α
␈↓αcomcond␈↓,␈αis␈αthat␈α
␈↓αcomcond␈↓␈αgenerates␈α
code␈αwhich␈αwhen␈α
executed␈αwill␈α
produce␈αthe␈αsame␈α
value
␈↓as␈α
the␈α
value␈α
produced␈α
by␈α
␈↓αevcond␈↓␈α
operating␈α
on␈α
the␈α
given␈α
S-expression.

␈↓The␈α
effect␈α
of␈α
␈↓αcomcond␈↓␈α
on␈α
a␈α
conditional␈α
of␈α
the␈α
form:

␈↓(␈↓↓*1*␈↓)␈↓ ∧_␈↓α(COND (␈↓p␈↓β1␈↓ e␈↓β1␈↓α) ... (␈↓p␈↓βn␈↓ e␈↓βn␈↓α))␈↓ should be clear. 

␈↓First␈αgenerate␈α
code␈αfor␈α
p␈↓β1␈↓;␈αgenerate␈α
a␈αtest␈α
for␈αtruth,␈αgoing␈α
to␈αthe␈α
code␈αfor␈α
e␈↓β1␈↓␈αif␈α
true,␈αand␈α
going␈αto
␈↓the␈α
code␈α∞for␈α
p␈↓β2␈↓␈α
if␈α∞not␈α
true.␈α∞ The␈α
code␈α
for␈α∞e␈↓β1␈↓␈α
must␈α
be␈α∞followed␈α
by␈α∞an␈α
exit␈α
from␈α∞the␈α
code␈α∞for␈α
the
␈↓conditional,␈αand␈αwe␈αshould␈αprobably␈αgenerate␈αan␈αerror␈αcondition␈αto␈αbe␈αexecuted␈αin␈αthe␈αcase␈αthat␈αp␈↓βn␈↓
␈↓is␈α
false.
␈↓␈↓↓140  Compilation␈↓ 57.3␈↓


␈↓Pictorially, we might represent the code as:

␈↓                <code for p␈↓β1␈↓>




␈↓                T                       NIL


␈↓        <code for e␈↓β1␈↓>               <code for p␈↓β2␈↓>


␈↓                                     T                  NIL


␈↓                <code for e␈↓β2␈↓>                       <code for p␈↓β3␈↓>

␈↓                     ....                               ....



␈↓                                             T                  NIL

␈↓                                <code for e␈↓βn␈↓>               <code for error>







␈↓This␈α
requires␈α∞the␈α
establishment␈α∞of␈α
more␈α∞conventions␈α
for␈α
our␈α∞compiler.␈α
 In␈α∞particular␈α
we␈α∞must␈α
be
␈↓able␈αto␈αtest␈αfor␈α␈↓αT␈↓␈α(or␈α␈↓αNIL␈↓).␈αWe␈αwill␈αdefine␈α␈↓αNIL␈↓␈αto␈αbe␈αthe␈αzero␈αof␈αthe␈αmachine,␈αand␈αwe␈αwill␈αtest␈αfor
␈↓␈↓αNIL␈↓␈αusing␈αthe␈α␈↓αJUMPE␈↓␈αinstruction␈α
(see␈αSection␈α6.18).␈αNext,␈αsince␈αour␈α
code␈αis␈αto␈αbe␈αa␈α
␈↓↓sequence␈↓␈αof
␈↓instructions,␈α⊃we␈α⊃must␈α⊃linearize␈α⊃this␈α⊃graph␈α⊃representation␈α⊃of␈α⊃the␈α⊃generated␈α⊃code.␈α⊃ Thus␈α∩for␈α⊃the
␈↓compilation␈α
of␈α
*1*,␈α
above,␈α
we␈α
will␈α
generate:
␈↓␈↓↓7.3␈↓ π←Compilers for subsets of LISP     141␈↓



␈↓␈↓ ¬_␈↓α(␈↓ ¬h␈↓<code for p␈↓β1␈↓>␈↓α
␈↓α␈↓ ¬_␈↓ ¬h(JUMPE AC1, L1)␈↓
␈↓␈↓ ¬_␈↓ ¬h<code for e␈↓β1␈↓>␈↓α
␈↓α␈↓ ¬_␈↓ ¬h(JRST L)
␈↓α␈↓ ¬_L1␈↓ ¬h␈↓<code for p␈↓β2␈↓>␈↓α
␈↓α␈↓ ¬_␈↓ ¬h(JUMPE AC1, L2)
␈↓α␈↓ ¬_        ...
␈↓α␈↓ ¬_Ln-1␈↓ ¬h␈↓<code for p␈↓βn␈↓>␈↓α
␈↓α␈↓ ¬_␈↓ ¬h(JUMPE AC1, Ln)␈↓
␈↓␈↓ ¬_␈↓ ¬h<code for e␈↓βn␈↓>␈↓α
␈↓α␈↓ ¬_␈↓ ¬h(JRST L)
␈↓α␈↓ ¬_Ln␈↓ ¬h(JRST ERROR)
␈↓α␈↓ ¬_L␈↓ ¬h   )

␈↓To␈αgenerate␈αsuch␈αcode␈αwe␈αneed␈αto␈αbe␈αable␈αto␈αgenerate␈αthe␈αlabels,␈α␈↓αLi␈↓.␈α The␈αgenerated␈α
labels␈αshould
␈↓be␈α
atomic␈α∞and␈α
should␈α∞be␈α
distinct.␈α
LISP␈α∞has␈α
a␈α∞function,␈α
␈↓αgensym␈↓,␈α
which␈α∞can␈α
be␈α∞used␈α
for␈α∞this␈α
task.
␈↓␈↓αgensym␈↓␈α∩is␈α∩a␈α∩function␈α∩of␈α∩no␈α∩arguments␈α∩whose␈α∩value␈α∩is␈α∩a␈α∩generated␈α∩symbol␈α∩or␈α∩atom␈α∩which␈α⊃is
␈↓guaranteed␈αto␈αbe␈αdistinct␈αfrom␈αany␈αatom␈αgenerated␈αin␈αany␈αof␈αthe␈αusual␈αmanners.␈αIn␈αmany␈αversions
␈↓of␈αLISP,␈αgensyms␈αare␈αof␈αthe␈α
form␈α␈↓αGn␈↓␈αwhere␈α␈↓αn␈↓␈αis␈αa␈α
four␈αdigit␈αnumber␈αbeginning␈αat␈α␈↓α0000␈↓.␈αThus␈α
the
␈↓first␈α⊃call;␈α⊃␈↓αgensym[␈α⊂]␈↓␈α⊃would␈α⊃give␈α⊃␈↓αG0000␈↓;␈α⊂succeeding␈α⊃calls␈α⊃would␈α⊂give␈α⊃␈↓αG0001,␈α⊃G0002␈↓,␈α⊃etc.␈α⊂ First,
␈↓gensyms␈α
are␈α
not␈α∞placed␈α
in␈α
the␈α∞object␈α
list␈α
since␈α
they␈α∞are␈α
usually␈α
used␈α∞only␈α
for␈α
their␈α∞unique␈α
name.
␈↓Second,␈αif␈αyou␈αexplicitly␈αplace␈αthem␈αin␈αthe␈α
symbol␈αtable␈αthey␈αare␈αobviously␈αdistinct␈αfrom␈αeach␈α
other
␈↓and␈α
will␈α
be␈α
distinct␈α
from␈α
all␈α
other␈α
atoms␈α
unless,␈α
of␈α
course,␈α
you␈α
read␈α
in␈α
such␈α
an␈α
atom.

␈↓Without␈α
too␈αmuch␈α
difficulty␈αwe␈α
can␈αnow␈α
add␈αthe␈α
recognizer␈αfor␈α
␈↓αCOND␈↓␈αto␈α
␈↓αcompexp␈↓␈α
and␈αconstruct
␈↓␈↓αcomcond␈↓.

␈↓α␈↓Add␈↓α␈α
eq[car[exp];COND]␈α
→␈α
comcond[cdr[exp];gensym[␈α
]];␈α
␈↓to␈↓α␈α
compexp␈α
␈↓where:

␈↓αcomcond <= λ[[u;l][prog[[z]␈↓ ∧hz ← gensym[ ];
␈↓α␈↓ ∧hreturn[␈↓ ¬X[null [u] → list[(JRST ERROR);l]:
␈↓α␈↓ ∧h␈↓ ¬X T → append␈↓ πλ[compexp[caar[u]];
␈↓α␈↓ ∧h␈↓ ¬X␈↓ πλ list[list[JUMPE;AC1;z]];
␈↓α␈↓ ∧h␈↓ ¬X␈↓ πλ compexp[cadar[u]];
␈↓α␈↓ ∧h␈↓ ¬X␈↓ πλ list[list[JRST;l]z];
␈↓α␈↓ ∧h␈↓ ¬X␈↓ πλ comcond[cdr[u];l]]]]
␈↓␈↓↓142  Compilation␈↓ 57.3␈↓


␈↓The partially exposed recursive structure of ␈↓αcomcond␈↓ would show:

␈↓␈↓ ∧`␈↓αcomcond[((␈↓p␈↓β1␈↓  e␈↓β1␈↓α) ...(␈↓p␈↓βn␈↓  e␈↓βn␈↓α));G0000]=

␈↓α␈↓ ¬_(␈↓␈↓ ε_<code for p␈↓β1␈↓>␈↓α
␈↓α␈↓ ¬_␈↓ ε_(JUMPE AC1, G0001)␈↓
␈↓␈↓ ¬_␈↓ ε_<code for e␈↓β1␈↓>␈↓α
␈↓α␈↓ ¬_␈↓ ε_(JRST G0000)
␈↓α␈↓ ¬_ G0001␈↓ ε_comcond[((␈↓p␈↓β2␈↓ e␈↓β2␈↓α) ...(␈↓p␈↓βn␈↓ e␈↓βn␈↓α))])


␈↓So␈αthe␈αsecond␈αargument␈αto␈α␈↓αcomcond␈↓␈αis␈αa␈αgenerated␈α
symbol␈αto␈αreflect␈αthe␈αlabel␈αattached␈αto␈αthe␈αend␈α
of
␈↓the␈α
conditional␈α∞statement.␈α
There␈α∞is␈α
another␈α∞generated␈α
name␈α
used␈α∞"locally"␈α
to␈α∞␈↓αJUMPE␈↓␈α
from␈α∞p␈↓βi␈↓␈α
to
␈↓p␈↓βi+1␈↓.

␈↓Before␈αattacking␈αthe␈αmajor␈αproblem␈αof␈α
our␈αcompilers,␈αthe␈αhandling␈αof␈αvariables,␈αwe␈α
shall␈αdescribe
␈↓how␈α
the␈α
list␈α
representing␈α
the␈α
output␈α
of␈α
the␈α
compiler␈α
is␈α
turned␈α
into␈α
code␈α
which␈α
runs␈α
on␈α
the␈α
machine.



␈↓␈↓ ¬t␈↓↓7.4  Problems␈↓

␈↓I. Evaluate:
␈↓␈↓αcompexp[(COND(␈↓ β8(EQ (QUOTE A)(QUOTE B))(QUOTE C))
␈↓α␈↓ β8((NULL (QUOTE A))(QUOTE FOO))))]


␈↓α****more, more ****




␈↓␈↓ ¬(␈↓↓7.5  One-pass Assemblers␈↓


␈↓Define␈α
␈↓αcompile␈↓␈α
as:

␈↓␈↓αcompile␈α
<=␈α
λ[[fn;vars;exp]append[list[list[LAP;fn;SUBR]];compexp[exp]]]␈↓.

␈↓Consider␈α
the␈α
output␈α
from␈α
␈↓αcompile␈↓␈α
for␈α
the␈α
function:

␈↓␈↓α␈↓ ¬Vj [] <= f[g[A];h[B]]␈↓ 

␈↓or␈α
more␈α
precisely,␈α
for␈α
the␈α
evaluation␈α
of

␈↓␈↓ ∧↔␈↓αcompile[J;();(F(G(QUOTE A))(H(QUOTE B)))]␈↓. 
␈↓␈↓↓7.5␈↓ λbOne-pass Assemblers     143␈↓


␈↓It␈α
would␈α
be␈α
a␈α
list␈α
of␈α
the␈α
form:
␈↓α␈↓ αX((LAP J SUBR)␈↓ πλ␈↓; says ␈↓αj␈↓ is a function␈↓α
␈↓α␈↓ αX(MOVEI AC1(QUOTE A))␈↓ πλ␈↓; make argument for call on ␈↓αg
␈↓α␈↓ αX(CALL 1 (E G))␈↓ πλ␈↓; call the function␈↓α
␈↓α␈↓ αX(PUSH P AC1)␈↓ πλ␈↓; save the value␈↓α
␈↓α␈↓ αX(MOVE AC1(QUOTE B))
␈↓α␈↓ αX(CALL 1 (E H))
␈↓α␈↓ αX(PUSH P AC1)
␈↓α␈↓ αX(POP P AC2)
␈↓α␈↓ αX(POP P AC1)
␈↓α␈↓ αX(CALL 2(E F))
␈↓α␈↓ αX(POPJ P)
␈↓α␈↓ αX)


␈↓As␈α∞you␈α∞know␈α∞the␈α∂machine␈α∞representation␈α∞of␈α∞these␈α∞instructions␈α∂are␈α∞encodings␈α∞of␈α∞specific␈α∂fields␈α∞of
␈↓specific␈αmachine␈αlocations␈αwith␈α
specific␈αnumbers.␈α For␈αexample,␈α
the␈αoperation␈α␈↓αPUSH␈↓␈αis␈α
represented
␈↓as␈α
a␈α
certain␈α
number,␈α
called␈α
its␈α␈↓↓operation␈α
code␈↓␈α
or␈α
␈↓↓op␈α
code␈↓,␈α
and␈αwhich␈α
will␈α
occupy␈α
a␈α
certain␈α
area␈αof␈α
a
␈↓machine␈α
word␈αso␈α
that␈αthe␈α
CPU␈α
can␈αinterpret␈α
it␈αas␈α
an␈α
instruction␈αto␈α
push␈αsomething␈α
onto␈α
a␈αstack.
␈↓Other␈αfields␈αin␈αthe␈αinstruction␈αare␈αto␈αbe␈αinterpreted␈αas␈αreferences␈αto␈αstacks,␈αto␈αmemory␈αlocations,␈αto
␈↓accumulators,␈αconstnats␈αor␈αexternal␈αreferences␈αto␈αother␈αroutines.␈α The␈αpurpose␈αof␈αan␈αassembler␈αis␈αto
␈↓translate␈α
these␈α
mnemonic␈α
instructions␈α
into␈α
raw␈α
seething␈α
machine␈α
instructions.

␈↓Essentially␈α
all␈α∞that␈α
the␈α∞assembler␈α
need␈α
do␈α∞is␈α
search␈α∞symbol␈α
tables␈α
for␈α∞the␈α
opcodes,␈α∞for␈α
subroutine
␈↓names,␈αfor␈αaccumulator␈αand␈αstack␈αnames,␈αand␈α
store␈αthe␈αresulting␈αvalues␈αin␈αthe␈αappropriate␈α
machine
␈↓locations.

␈↓Things␈αare␈αslightly␈αmore␈αcomplex:␈αwe␈αmust␈α
also␈α␈↓¬add␈↓␈αinformation␈αto␈αthe␈αtables␈αas␈αwe␈α
proceed.␈α For
␈↓example,␈αas␈αwe␈αassemble␈αthe␈αcode␈αfor␈αa␈αnew␈αroutine␈αwe␈αmust␈αadd␈αits␈αname␈αand␈αstarting␈αaddress␈αto
␈↓the␈α
current␈α
symbol␈α
table.␈α
 The␈α
phrase,␈α
␈↓α␈α
(LAP␈α
␈↓name␈↓α␈α
SUBR)␈↓␈α
does␈α
this.

␈↓We␈α⊂must␈α∂exercise␈α⊂a␈α∂bit␈α⊂of␈α∂care␈α⊂in␈α∂handling␈α⊂␈↓αQUOTE␈↓d␈α∂expressions.␈α⊂ Assembling␈α∂a␈α⊂construct␈α∂like
␈↓␈↓α(MOVEI␈α∞AC1␈α
(QUOTE␈α∞(A␈α
B␈α∞C)))␈↓␈α
should␈α∞have␈α∞the␈α
effect␈α∞of␈α
constructing␈α∞the␈α
list␈α∞␈↓α(A␈α
B␈α∞C)␈↓␈α∞in␈α
free
␈↓space␈α∞and␈α
placing␈α∞an␈α∞instruction␈α
in␈α∞memory␈α∞to␈α
load␈α∞the␈α∞address␈α
of␈α∞this␈α∞list␈α
into␈α∞AC1.␈α∞ What␈α
we
␈↓must␈α
notice␈αis␈α
that␈αthis␈α
list␈α
␈↓α(A␈αB␈α
C)␈↓␈αis␈α
subject␈αto␈α
garbage␈α
collection␈αand,␈α
if␈αleft␈α
unprotected,␈αcould␈α
be
␈↓destroyed.␈α⊃ There␈α⊃are␈α⊃a␈α⊃couple␈α⊃of␈α⊃solutions.␈α⊃Perhaps␈α⊃the␈α⊃garbage␈α⊃collector␈α⊃could␈α⊃look␈α⊂through
␈↓compiled␈αcode␈αfor␈αany␈αreferences␈αto␈α
free-␈αor␈αfull-word-␈αspace;␈αor␈αwe␈α
could␈αmake␈αa␈αlist␈αof␈αall␈αof␈α
these
␈↓constants␈α
and␈α
let␈α
the␈α
garbage␈α
collector␈α
mark␈α
the␈α
list.

␈↓Much␈αmore␈α
problematic␈αis␈α
the␈αhandling␈α
of␈αlabels␈αand␈α
references␈αto␈α
labels.␈α This␈α
case␈αarose␈α
in␈αthe
␈↓compiler␈α∩for␈α∩␈↓αtgmoafr␈↓␈α∩when␈α∩we␈α∩introduced␈α∩conditional␈α∩expressions.␈α∩Recall␈α∩that␈α∩the␈α∩code␈α∩for␈α∩a
␈↓conditional␈α
expression␈α
of␈α
the␈α
form:
␈↓␈↓↓144  Compilation␈↓ 47.5␈↓



␈↓␈↓α␈↓ ¬ε(COND (␈↓p␈↓β1␈↓ e␈↓β1␈↓α) ... (␈↓p␈↓βn␈↓ e␈↓βn␈↓α)) ␈↓ was: 

␈↓␈↓ ¬h    <code for p␈↓β1␈↓>
␈↓␈↓ ¬h    ␈↓α(JUMPE AC1 L1)␈↓
␈↓␈↓ ¬h    <code for e␈↓β1␈↓>
␈↓␈↓ ¬h    ␈↓α(JRST L)
␈↓α␈↓ ¬hL1  ␈↓<code for p␈↓β2␈↓>
␈↓␈↓ ¬h    ␈↓α(JUMPE AC1 L2)␈↓

␈↓␈↓ ¬h          ....
␈↓␈↓ ¬h    <code for e␈↓βn␈↓>

␈↓␈↓ ¬h␈↓αL        ....


␈↓The␈αsymbols,␈α␈↓α␈αL,␈αL1,␈↓␈αand␈α␈↓αL2␈↓␈αin␈αthis␈αexample␈αare␈αlabels.␈α Notice␈αthat␈αif␈αwe␈αwere␈αto␈αconsider␈αwriting
␈↓the␈α⊃assembler␈α⊃in␈α⊃LISP,␈α∩we␈α⊃could␈α⊃distinguish␈α⊃occurrences␈α∩of␈α⊃labels␈α⊃from␈α⊃instructions␈α∩using␈α⊃the
␈↓predicate,␈α
␈↓αatom␈↓.

␈↓It␈αperhaps␈αis␈αtime␈αto␈αstart␈αthinking␈αabout␈αwriting␈αsuch␈αan␈αassembler.␈α One␈αof␈αthe␈αarguments␈αto␈αthe
␈↓function␈α⊃should␈α∩be␈α⊃the␈α⊃list␈α∩representation␈α⊃of␈α⊃the␈α∩program.␈α⊃ One␈α⊃of␈α∩its␈α⊃arguments␈α∩should␈α⊃also
␈↓describe␈αwhere␈α(in␈αBPS)␈αwe␈αwish␈αthe␈αassembled␈αcode␈αto␈αbe␈αlocated.␈αWe␈αshould␈αalso␈αhave␈αaccess␈αto
␈↓an␈α∪initial␈α∪symbol␈α∪table,␈α∪describing␈α∪the␈α∩opcodes␈α∪and␈α∪pre-defined␈α∪symbol␈α∪names.␈α∪Let's␈α∪call␈α∩the
␈↓function,␈α␈↓αassemble␈↓.␈α ␈↓αassemble␈↓␈αthen␈αcan␈αgo␈α␈↓αcdr␈↓-ing␈αdown␈αthe␈αprogram␈αlist,␈αlooking␈αup␈αdefinitions␈αand
␈↓manufacturing␈α∞the␈α∞numerical␈α∂equivalent␈α∞of␈α∞each␈α∞instruction,␈α∂then␈α∞depositing␈α∞that␈α∞number␈α∂in␈α∞the
␈↓appropriate␈α↔machine␈α_location.␈α↔ If␈α_␈↓αassemble␈↓␈α↔comes␈α↔across␈α_a␈α↔label␈α_definition,␈α↔it␈α_should␈α↔add
␈↓information␈α
to␈α
a␈α
symbol␈α
table,␈α
reflecting␈α
that␈α
the␈α
label␈α
has␈α
been␈α
seen␈α
and␈α
that␈α
it␈α
is␈α
associated␈αwith␈α
a
␈↓specific␈αlocation␈αin␈αmemory.␈α Then␈αfuture␈αreferences␈αto␈αthat␈αlabel␈αcan␈αbe␈αtranslated␈αto␈αreferences␈αto
␈↓the␈αassociated␈α
machine␈αlocation.␈α
 The␈αonly␈α
problem␈αis␈α
that␈αreferences␈α
to␈αlabels␈α
may␈αoccur␈α␈↓¬before␈↓␈α
we
␈↓have␈α∀come␈α∀across␈α∃the␈α∀label␈α∀definition␈α∃in␈α∀the␈α∀program.␈α∃ Such␈α∀references␈α∀are␈α∃called␈α∀␈↓↓forward
␈↓↓references␈↓.

␈↓For␈α
example,␈α
consider␈α
the␈α
following␈α
nonsense␈α
program:

␈↓α␈↓ ¬h(  (LAP FOO SUBR)
␈↓α␈↓ ¬h X (JRST X)
␈↓α␈↓ ¬h    (JRST Y)
␈↓α␈↓ ¬h Y (JRST X) )


␈↓The␈α∂reference␈α∂to␈α∞␈↓αY␈↓␈α∂is␈α∂a␈α∞forward␈α∂reference;␈α∂neither␈α∞of␈α∂the␈α∂references␈α∞to␈α∂␈↓αX␈↓␈α∂is␈α∞forward␈α∂since␈α∂␈↓αX␈↓␈α∞is
␈↓defined␈α
before␈α
being␈α
referenced.

␈↓There␈α
are␈α
two␈α
solutions␈α
to␈α
the␈α
forward␈α
reference␈α
problem:
␈↓␈↓↓7.5␈↓ λbOne-pass Assemblers     145␈↓


␈↓␈↓↓1.␈↓␈α∞Make␈α∞two␈α∞passes␈α∞through␈α∞the␈α∞input␈α∞program.␈α∞ The␈α∞first␈α∞pass␈α∞decides␈α∞where␈α∞the␈α∞labels␈α∂will␈α∞be
␈↓␈↓ α_assigned␈α
in␈αmemory.␈α
 That␈αis,␈α
this␈αpass␈α
builds␈α
a␈αsymbol␈α
table␈αof␈α
labels␈αand␈α
locations.␈α Then␈α
a
␈↓␈↓ α_second␈α⊂pass␈α⊂uses␈α⊂this␈α∂symbol␈α⊂table␈α⊂to␈α⊂actually␈α⊂assemble␈α∂the␈α⊂code␈α⊂into␈α⊂the␈α⊂machine.␈α∂ This
␈↓␈↓ α_works,␈α∩but␈α∩is␈α∪not␈α∩particularly␈α∩elegant.␈α∪It␈α∩is␈α∩expensive␈α∩to␈α∪read␈α∩through␈α∩the␈α∪input␈α∩twice,
␈↓␈↓ α_particularly␈α
when␈α
we␈α
can␈α
do␈α
better.

␈↓␈↓↓2.␈↓␈α
The␈αother␈α
solution␈α
is␈αto␈α
make␈α
one␈αclever␈α
pass␈α
through␈αthe␈α
input.␈α
 Whenever␈αwe␈α
come␈α
across␈αa
␈↓␈↓ α_forward␈α∞reference␈α∞we␈α∞add␈α∞information␈α∞to␈α∞the␈α∞symbol␈α∞table␈α∞telling␈α∞that␈α∞a␈α∞forward␈α∞reference
␈↓␈↓ α_has␈α
occurred␈α
at␈α
this␈α
location.␈α We␈α
assemble␈α
as␈α
much␈α
of␈αthe␈α
instruction␈α
as␈α
we␈α
can.␈α
 When␈αa
␈↓␈↓ α_label␈α⊂definition␈α⊂␈↓¬does␈↓␈α∂occur␈α⊂we␈α⊂check␈α⊂the␈α∂table␈α⊂to␈α⊂see␈α⊂if␈α∂there␈α⊂are␈α⊂any␈α⊂forward␈α∂references
␈↓␈↓ α_pending␈α
on␈α
that␈α
label.␈α It␈α
there␈α
are␈α
such␈α
we␈α␈↓¬fix-up␈↓␈α
those␈α
instructions␈α
to␈α
reference␈αthe␈α
location
␈↓␈↓ α_now␈α
assigned␈α
to␈α
the␈α
label.

␈↓A␈αspeed-up␈αby␈αa␈αfactor␈αranging␈αfrom␈αtwo␈αto␈αfive␈αis␈αnot␈αuncommon␈αfor␈αa␈αgood␈αone␈αpass␈αassembler.
␈↓There␈α∀are␈α∀some␈α∀restrictions␈α∀which␈α∀are␈α∀imposed␈α∀on␈α∀one-pass␈α∀assemblers,␈α∀but␈α∃particularly␈α∀for
␈↓assembling␈α
compiled␈α
code,␈α
one-pass␈α
assemblers␈α
are␈α
quite␈α
sufficient.

␈↓There␈α⊂are␈α⊂at␈α⊃least␈α⊂two␈α⊂ways␈α⊃to␈α⊂handle␈α⊂fixups␈α⊂and␈α⊃forward␈α⊂references.␈α⊂ If␈α⊃the␈α⊂fixups␈α⊂are␈α⊃of␈α⊂a
␈↓particularly␈α
simple␈α∞nature,␈α
say␈α∞only␈α
requiring␈α∞fixups␈α
to␈α∞the␈α
address-part␈α∞of␈α
a␈α∞word,␈α
then␈α∞we␈α
may
␈↓link␈α
those␈α
pending␈α
forward␈α
references␈α
together,␈α
chaining␈α
them␈α
on␈α
their␈α
address␈α
parts.␈α
Thus:





















␈↓Another␈αsolution,␈α
which␈αis␈α
potentially␈αmore␈α
general␈α(that␈αis,␈α
it␈αcould␈α
handle␈αleft-half,␈α
right-half␈αor
␈↓partial-word␈α
fixups)␈α
is␈α
to␈α
store␈α
the␈α
information␈α
about␈α
each␈α
fixup␈α
in␈α
the␈α
symbol␈α
table␈α∞under␈α
each
␈↓label.␈α
Thus:
␈↓␈↓↓146  Compilation␈↓ 47.5␈↓























␈↓Both␈α
methods␈α
are␈α
useful.␈α
Both␈α
have␈α
been␈α
used␈α
extensively␈α
in␈α
assemblers␈α
and␈α
compilers.

␈↓To␈α
write␈α
the␈α
function,␈α
␈↓αassemble␈↓,␈α
we␈α
will␈α
need␈α
two␈α
functions:

␈↓␈↓↓1.␈↓␈α∩␈↓αdeposit␈↓␈↓α[x;y]␈↓:␈α∪␈↓αx␈↓␈α∩represents␈α∪a␈α∩machine␈α∪address;␈α∩␈↓αy␈↓␈α∪is␈α∩a␈α∪list,␈α∩representing␈α∪the␈α∩instruction␈α∪to␈α∩be
␈↓␈↓ αhdeposited.␈α∞␈↓αy␈↓␈α
should␈α∞be␈α
a␈α∞list␈α∞of␈α
four␈α∞elements:␈α
␈↓α(␈↓opcode␈α∞,accumulator␈α∞number,␈α
memory
␈↓␈↓ αhaddress,␈α
index␈α
register␈↓α)␈↓␈α
The␈α
value␈α
of␈α
␈↓αdeposit␈↓␈α
is␈α
the␈α
value␈α
of␈α
␈↓αy␈↓.

␈↓␈↓↓2.␈↓␈α
␈↓αexamine␈↓␈↓α[x]␈↓:␈α
␈↓αx␈↓␈α
represents␈α
a␈α
machine␈α
address.␈α
 The␈αvalue␈α
of␈α
␈↓αexamine␈↓␈α
is␈α
the␈α
contents␈α
of␈α
location␈α␈↓αx␈↓␈α
in
␈↓␈↓ αhthe␈α
form␈α
of␈α
a␈α
list␈α
as␈α
specified␈α
above.

␈↓␈↓αassemble␈↓␈α
needs␈α
to␈α
recognize␈α
that␈α
there␈α
are␈α
different␈α
instruction␈α
formats.␈α
 That␈α
is,some␈α
instructions
␈↓use␈α⊃an␈α⊂opcode␈α⊃and␈α⊂a␈α⊃memory␈α⊂reference:␈α⊃␈↓α(JRST L)␈↓;␈α⊂some␈α⊃use␈α⊂an␈α⊃opcode,␈α⊂accumulator,␈α⊃and␈α⊂an
␈↓address:␈α_␈↓α(PUSH P AC1)␈↓;␈α_and␈α_some␈α_vary:␈α_␈↓α(MOVE AC1 (QUOTE␈α_A))␈↓␈α_and␈α↔␈↓α(MOVE AC1 -1 P)␈↓.
␈↓␈↓αassemble␈↓␈α⊃also␈α⊃has␈α⊂to␈α⊃have␈α⊃an␈α⊃initial␈α⊂symbol␈α⊃table␈α⊃of␈α⊂opcodes,␈α⊃accumulator␈α⊃numbers,␈α⊃and␈α⊂stack
␈↓numbers:
␈↓␈↓↓7.5␈↓ λbOne-pass Assemblers     147␈↓


␈↓␈↓↓␈↓ ¬hsymbol␈↓ πλvalue␈↓α
␈↓α␈↓ ¬hMOVE␈↓ πλ200
␈↓α␈↓ ¬hMOVEI␈↓ πλ201
␈↓α␈↓ ¬hSUB␈↓ πλ274
␈↓α␈↓ ¬hJRST␈↓ πλ254
␈↓α␈↓ ¬hJUMPE␈↓ πλ322
␈↓α␈↓ ¬hJUMPN␈↓ πλ326
␈↓α␈↓ ¬hPUSH␈↓ πλ261
␈↓α␈↓ ¬hPOP␈↓ πλ262
␈↓α␈↓ ¬hPOPJ␈↓ πλ263
␈↓α␈↓ ¬hCALL␈↓ πλ034
␈↓α␈↓ ¬hAC1-n␈↓ πλ1 - n
␈↓α␈↓ ¬hP␈↓ πλ14

␈↓So␈α
for␈α
example:

␈↓␈↓ αD␈↓αassemble[((LAP FOO SUBR) X (JRST X) (JRST Y) Y (JRST X));104]␈↓ should 

␈↓have␈α
the␈α
final␈α
effect:

␈↓--→␈↓εα~~~]␈↓--→␈↓
PNAME␈↓ε[~~[~~]␈↓--→␈↓ε[~~]~~]␈↓--→␈↓
SUBR␈↓ε[~~[~~]␈↓...
␈↓␈↓ εh|
␈↓␈↓ εh|
␈↓␈↓ εh|
␈↓␈↓ εh|        op   ac  ind  add
␈↓␈↓ εh→104  254    0   0   104
␈↓␈↓ εh  105  254    0   0   106
␈↓␈↓ εh  106  254    0   0   104




␈↓␈↓ ¬␈↓↓7.6  A compiler for simple ␈↓αeval␈↓↓␈↓α


␈↓Consider␈α
the␈α
function␈α
defined␈α
by:

␈↓␈↓ ¬O␈↓αj[x;y] <= f[g[x];h[y]]␈↓ 

␈↓We␈α
claim␈α
that␈α
the␈α
following␈α
code␈α
suffices␈α
for␈α
function:

␈↓α␈↓ αX(LAP J SUBR)␈↓ πλ␈↓; says ␈↓αj␈↓ is a function␈↓α
␈↓α␈↓ αX(PUSH P AC1)␈↓ πλ␈↓; save the input args␈↓α
␈↓α␈↓ αX(PUSH P AC2)
␈↓α␈↓ αX(MOVE AC1 -1 P)␈↓ πλ␈↓; get ␈↓αx
␈↓α␈↓ αX(CALL 1 (E G))␈↓ πλ␈↓; call the function named ␈↓αg
␈↓␈↓↓148  Compilation␈↓ 57.6␈↓


␈↓α␈↓ αX(PUSH P AC1)␈↓ πλ␈↓; save the value␈↓α
␈↓α␈↓ αX(MOVE AC1 -1 P)␈↓ πλ␈↓; get y␈↓α
␈↓α␈↓ αX(CALL 1(E H))␈↓ πλ␈↓; call ␈↓αh
␈↓α␈↓ αX(PUSH P AC1)
␈↓α␈↓ αX(POP P AC2)␈↓ πλ␈↓; restore the arguments in␈↓α
␈↓α␈↓ αX(POP P AC1)␈↓ πλ␈↓;   preparation for␈↓α
␈↓α␈↓ αX(CALL 2(E F))␈↓ πλ␈↓;   calling ␈↓αf
␈↓α␈↓ αX(SUB P(C 0 0 2 2))␈↓ πλ; ␈↓sync the stack; remove the saved args␈↓α
␈↓α␈↓ αX(POPJ P)␈↓ πλ; ␈↓exit.␈↓α AC1␈↓ still has the value from ␈↓αf.
␈↓α␈↓ αX   )

␈↓Here␈α
is␈α
a␈α
picture␈α
of␈α
the␈α
execution␈α
of␈α
the␈α
code:

␈↓αAC1: x ; AC2: y

␈↓α␈↓ αλ|␈↓ αX| (PUSH P AC1)␈↓ ∧H␈↓ ¬_ (PUSH P AC2)␈↓ πλ|  y␈↓ πX|(MOVE AC1 -1 P)␈↓ 	H| y | (CALL 1 (E G))
␈↓α␈↓ αλ|␈↓ αX|    =>␈↓ ∧H|  x␈↓ ¬_|     =>␈↓ πλ|   x␈↓ πX|        =>␈↓ 	H| x |  =>


␈↓αAC1: g[x] ; AC2: ?␈↓ ∧H␈↓ ¬_␈↓ πλAC1: y ; AC2: ?
␈↓α␈↓ αλ␈↓ αX (PUSH P AC1)␈↓ ∧H|g[x]␈↓ ¬_|(MOVE AC1 -1 P)␈↓ πλ|g[x]␈↓ πX| (CALL 1(E H))
␈↓α␈↓ αλ|  y␈↓ αX|      =>␈↓ ∧H|  y␈↓ ¬_|      =>␈↓ πλ|  y␈↓ πX|   =>
␈↓α␈↓ αλ|  x␈↓ αX|␈↓ ∧H|  x␈↓ ¬_|␈↓ πλ|  x␈↓ πX|


␈↓αAC1: h[y] ; AC2: ?␈↓ ∧H␈↓ ¬_␈↓ πλAC2: h[y]␈↓ 	HAC1: g[x] ; AC2: h[y]
␈↓α␈↓ αλ|g[x]␈↓ αX| (PUSH P AC1)␈↓ ∧H|h[y]␈↓ ¬_|   (POP P AC2)␈↓ πλ|g[x]␈↓ πX|  (POP P AC1)␈↓ 	H| y |(CALL 2 (E F))
␈↓α␈↓ αλ|  y␈↓ αX|       =>␈↓ ∧H|g[x]␈↓ ¬_|      =>␈↓ πλ|  y␈↓ πX|      =>␈↓ 	H| x |   =>
␈↓α␈↓ αλ|  x␈↓ αX|␈↓ ∧H|  y␈↓ ¬_|␈↓ πλ|  x␈↓ πX|
␈↓α␈↓ αλ␈↓ αX␈↓ ∧H|  x␈↓ ¬_|


␈↓αAC1: f[g[x];h[y]]
␈↓α␈↓ αλ|  y␈↓ αX|(SUB P (C 0 0 2 2))␈↓ ¬_␈↓ πλ|␈↓ πX| (POPJ P)
␈↓α␈↓ αλ|  x␈↓ αX|           =>␈↓ ∧H=>



␈↓Notes:␈αThe␈α
␈↓α(MOVE␈α␈↓x␈α
-n␈↓α␈αP)␈↓␈α
instruction␈αallows␈α
us␈αto␈α
put␈αa␈α
copy␈αof␈α
the␈αcontents␈α
of␈αthe␈α
n␈↓πth␈↓␈αelement
␈↓␈↓ αhdown␈αthe␈αstack.␈α
 Notice␈αtoo,␈αthat␈α
the␈αaddressing␈αin␈αthe␈α
code␈αis␈αrelative␈α
to␈αthe␈αtop␈αof␈α
the
␈↓␈↓ αhstack:␈α␈↓α(MOVE AC1 -1 P)␈↓␈αgets␈α
us␈α␈↓αx␈↓␈αin␈α
one␈αinstance␈αand␈α
gets␈αus␈α␈↓αy␈↓␈α
in␈αanother,␈αbecause␈α
the
␈↓␈↓ αhtop␈α
of␈α
the␈α
stack␈α
changes.

␈↓Clearly␈α∞we␈α∞want␈α∞a␈α∞compiler␈α∞to␈α∞produce␈α∞equivalent␈α∞(albeit␈α∞inefficient)␈α∞code.␈α∞ Once␈α∞we␈α∞understand
␈↓how␈α
to␈α
do␈α
this␈αit␈α
is␈α
relatively␈α
easy␈α
to␈αimprove␈α
on␈α
the␈α
efficiency.␈α We␈α
shall␈α
now␈α
extend␈α
␈↓αcompile␈↓␈αto
␈↓handle␈α
local␈α
variables.
␈↓␈↓↓7.6␈↓ λ⊃A compiler for simple ␈↓αeval␈↓↓     149␈↓α


␈↓The␈α⊃major␈α⊂failing␈α⊃of␈α⊂the␈α⊃previous␈α⊂␈↓αcompile␈↓␈α⊃is␈α⊂its␈α⊃total␈α⊂lack␈α⊃of␈α⊂interest␈α⊃in␈α⊂variables.␈α⊃ This␈α⊃is␈α⊂a
␈↓non-trivial␈αproblem␈αwhich␈αevery␈αcompiler␈αmust␈αface.␈α You␈αhave␈αjust␈αseen␈αan␈αexample␈α
of␈αplausible
␈↓code␈α
generation␈α
for␈α
simple␈α
LISP␈α
functions,␈α
in␈α
particular:

␈↓␈↓ ¬V␈↓αj[x;y] = f[g[x];h[y]]␈↓ 

␈↓The␈αcrucial␈αpoint␈αis␈αhow␈αto␈αgenerate␈αcode␈αwhich␈α`knows'␈αwhere␈αon␈αthe␈αstack␈αwe␈αcan␈αfind␈αthe␈αvalue
␈↓of␈αa␈α(local)␈αvariable␈αwhen␈αwe␈αexecute␈αthat␈αcode.␈α What␈αwe␈αshall␈αdo␈αis␈αsimulate␈αthe␈αbehavior␈αof␈αthe
␈↓runtime␈αstack␈αwhile␈αwe␈αare␈αcompiling␈αthe␈αcode.␈α The␈αcompiler␈αcannot␈αknow␈αwhat␈αthe␈α␈↓↓values␈↓␈αof␈αthe
␈↓variables␈α
will␈α
be␈α
at␈α
runtime␈α
but␈α
we␈α
claim␈α
that␈α
it␈α
should␈α
know␈α
␈↓αwhere␈↓␈α
to␈α
find␈α
the␈α
values.␈α
 We␈αwill
␈↓carry␈α∞this␈α∞information␈α∞through␈α∞the␈α∞compiler␈α∂in␈α∞a␈α∞manner␈α∞reminiscent␈α∞of␈α∞the␈α∂`dotted-pair'␈α∞symbol
␈↓table␈α
of␈α
the␈α
first␈α
version␈α
of␈α␈↓αeval␈↓␈α
seen␈α
in␈α
Section␈α
4.7.␈α
 Instead␈αof␈α
posting␈α
the␈α
current␈α
values␈α
in␈αthe
␈↓stack,␈αthe␈αcompiler␈αwill␈αpost␈αinformation␈αabout␈αthe␈αpositions␈αof␈αthe␈αvariables␈αrelative␈αto␈αthe␈αtop␈αof
␈↓the␈α
stack␈αat␈α
the␈αtime␈α
we␈α
enter␈αthe␈α
function.␈α The␈α
variable␈α
␈↓αvpl␈↓,␈αfor␈α
variable␈αpair␈α
list,␈α
contains␈αthis
␈↓information.␈α
 That␈α
is␈α
if␈α
we␈αare␈α
to␈α
compile␈α
a␈α
function␈αwith␈α
λ-variables,␈α
␈↓α[x;y;z]␈↓␈α
then␈α
␈↓αvpl␈↓␈α
will␈αbegin
␈↓with:

␈↓␈↓ ¬3␈↓α((X . 1), (Y . 2), (Z .3) ...␈↓ 

␈↓When␈αwe␈αset␈α
up␈α␈↓αvpl␈↓␈αwe␈αalso␈α
set␈αthe␈α␈↓↓offset␈↓,␈α
called␈α␈↓αm␈↓,␈αto␈αminus␈α
the␈αnumber␈αof␈α
args␈αadded␈αto␈α␈↓αvpl␈↓.␈α
(In
␈↓this␈α∀case␈α∃-3).␈α∀ Now␈α∃if␈α∀we␈α∃come␈α∀across␈α∃a␈α∀reference␈α∃say␈α∀to␈α∃␈↓αY␈↓␈α∀while␈α∃compiling␈α∀code,␈α∃we␈α∀use
␈↓␈↓αcdr[assoc[Y;vpl]]␈↓␈αto␈αretrieve␈α␈↓α2␈↓.␈α The␈αoffset␈αplus␈αthis␈αretrieved␈αvalue␈αgives␈αus␈αthe␈αrelative␈αposition␈αof
␈↓␈↓αY␈↓␈α
in␈αthe␈α
stack.␈α
 I.␈αe.,␈α
-3␈α
+␈α2␈α
=␈α
-1.␈α Thus␈α
to␈α
refer␈αto␈α
the␈α
stack␈αlocation␈α
of␈α
␈↓αY␈↓␈αwe␈α
could␈α
use␈α␈↓α(....-1 , P)␈↓.
␈↓What␈α⊂happens␈α⊂as␈α⊂we␈α⊂add␈α⊃elements␈α⊂to␈α⊂the␈α⊂stack?␈α⊂(Or␈α⊃to␈α⊂be␈α⊂more␈α⊂precise...what␈α⊂happens␈α⊃as␈α⊂we
␈↓generate␈α∞code␈α∞which␈α∞when␈α∞executed␈α∞will␈α∞add␈α∂elements␈α∞to␈α∞the␈α∞stack.)␈α∞Clearly␈α∞we␈α∞must␈α∂modify␈α∞the
␈↓offset.␈α If␈αwe␈αadd␈α
one␈αelement,␈αwe␈αwould␈α
set␈α␈↓αm␈↓␈αto␈α-4.␈α
 Then␈αto␈αreference␈α␈↓αY␈↓␈α
now␈αuse␈α-4␈α+␈α
2␈α=␈α-2.␈α I.␈α
e.
␈↓a␈α
reference␈α
to␈α
␈↓αY␈↓␈α
is␈α
now␈α
of␈α
the␈α
form:

␈↓␈↓ ε∞␈↓α( ......-2 P).␈↓ 

␈↓But␈α∞that's␈α∞right.␈α∞ ␈↓αY␈↓␈α∞is␈α∞now␈α
further␈α∞down␈α∞in␈α∞the␈α∞run␈α∞time␈α
stack.␈α∞ So␈α∞to␈α∞complete␈α∞the␈α∞analogy,␈α
the
␈↓`symbol␈α
table'␈α
is␈α
really␈α
defined␈α
by␈α
␈↓αm␈↓␈α
plus␈α
the␈α
current␈α
␈↓αvpl␈↓.

␈↓When␈α
will␈α
the␈α
compiler␈αmake␈α
modifications␈α
to␈α
the␈αtop␈α
of␈α
the␈α
stack?␈α First,␈α
we␈α
push␈α
new␈αelements
␈↓when␈αwe␈αare␈αcompiling␈αthe␈αarguments␈αto␈αa␈αfunction␈αcall.␈α The␈αfunction␈αin␈αthe␈αnew␈αcompiler␈αwhich
␈↓compiles␈α
the␈α
argument␈α
list␈α
is␈α
called␈α
␈↓αcomplis␈↓:

␈↓αcomplis <= λ[[u;m;vpl]␈↓ ∧_[null [u] → NIL
␈↓α␈↓ ∧_ T → append␈↓ ¬8[compexp [car [u]; m; vpl];
␈↓α␈↓ ∧_␈↓ ¬8 ((PUSH P AC1));
␈↓α␈↓ ∧_␈↓ ¬8 complis [cdr [u]; m-1; vpl]]]

␈↓Notice␈α→that␈α→␈↓αcomplis␈↓␈α→compiles␈α→the␈α~arguments␈α→from␈α→left␈α→to␈α→right,␈α→interspursing␈α~them␈α→with
␈↓␈↓α(PUSH P AC1)␈↓␈α∂and␈α∞recurring␈α∂with␈α∞a␈α∂new␈α∞offset␈α∂reflecting␈α∞the␈α∂effect␈α∞of␈α∂the␈α∞␈↓αPUSH␈↓.␈α∂Clearly␈α∞this
␈↓function␈α
is␈α
analogous␈α
to␈α
␈↓αevlis␈↓.
␈↓␈↓↓150  Compilation␈↓ 57.6␈↓


␈↓Here's␈α
a␈α
brief␈α
description␈α
of␈α
the␈α
parts␈α
of␈α
the␈α
new␈α
compiler.␈α
This␈α
compiler␈α
was␈α
adapted␈α
from␈αone
␈↓written␈αby␈αJohn␈αMcCarthy␈αin␈αconjunction␈αwith␈αa␈αcourse␈αat␈αStanford␈αUniversity␈αentitled␈α␈↓αComputing
␈↓αwith␈αSymbolic␈αExpressions␈↓.␈αThe␈α
McCarthy␈αcompiler␈αwas␈αalso␈αthe␈α
subject␈αof␈αstudy␈αby␈αRalph␈α
London
␈↓in␈α
his␈α
paper,␈α
␈↓αCorrectness␈α
of␈α
Two␈α
Compilers␈α
for␈α
a␈α
LISP␈α
Subset␈↓.

␈↓␈↓αcompile[fn;vars;exp]:␈α⊂fn␈↓␈α⊂is␈α⊂the␈α⊂name␈α∂of␈α⊂the␈α⊂function␈α⊂to␈α⊂be␈α∂compiled.␈α⊂␈↓αvars␈↓␈α⊂is␈α⊂the␈α⊂list␈α⊂of␈α∂lambda
␈↓␈↓ β8variables.␈α
 ␈↓αexp␈↓␈α
is␈α
the␈α
lambda-body.

␈↓␈↓αprup[vars;n]:␈α
vars␈↓␈α
is␈α
a␈α
lambda␈α
list,␈α
␈↓αn␈↓␈α
is␈α
an␈α
integer.␈α
 ␈↓αprup␈↓␈α
builds␈α
a␈α
variable-pair␈α
list.

␈↓␈↓αmkpush[n;m]␈↓:␈α
generates␈α
a␈α
sequence␈α
of␈α
␈↓αPUSH␈↓␈α
instructions.

␈↓␈↓αloadac[n;k]␈↓:␈α∞is␈α∂a␈α∞slight␈α∞modification␈α∂of␈α∞the␈α∞previous␈α∂␈↓αloadac␈↓␈α∞(page␈α∞139).␈α∂ This␈α∞version␈α∂generates␈α∞a
␈↓␈↓ β8series␈α
of␈α
␈↓α(MOVE␈αACi␈α
...)␈↓␈α
instructions␈α
followed␈αby␈α
a␈α
␈↓α(SUB␈α
P␈α(C␈α
0␈α
0␈α
N␈αN))␈↓,␈α
rather
␈↓␈↓ β8than␈α
a␈α
sequence␈α
of␈α
␈↓αPOP␈↓s.

␈↓␈↓αcompexp[exp;m;vpl]␈↓:␈αthis␈α
function␈αdoes␈αmost␈α
of␈αthe␈αwork.␈α
 It␈αis␈αanalogous␈α
to␈α␈↓αeval␈↓.␈α It␈α
generates␈αthe
␈↓␈↓ β8code␈αfor␈αconstants,␈αfor␈αa␈αreference␈αto␈αa␈αvariable,␈αand␈αfor␈αconditional␈αexpressions.
␈↓␈↓ β8It␈α
is␈αused␈α
for␈αcompiling␈α
code␈α
for␈αa␈α
call␈αon␈α
a␈α
function,␈αcompiling␈α
the␈αargument␈α
list
␈↓␈↓ β8with␈α∩␈↓αcomplis␈↓,␈α∩which␈α∩will␈α∩leave␈α∪the␈α∩arguments␈α∩in␈α∩the␈α∩stack;␈α∩␈↓αloadac␈↓␈α∪loads␈α∩the
␈↓␈↓ β8appropriate␈α
␈↓αAC␈↓'s.␈α
 and␈α
then␈α
we␈α
generate␈α
the␈α
␈↓αSUB␈↓␈α
to␈α
sync␈α
the␈α
stack,␈α
and␈αfinally
␈↓␈↓ β8generate␈α
a␈α
call␈α
on␈α
the␈α
function.

␈↓␈↓αcomcond[u;m;l;vpl]␈↓:␈αthis␈αcompiles␈α
the␈αbody␈αof␈α
conditional␈αexpressions.␈α ␈↓αu␈↓␈αis␈α
the␈αp␈↓βi␈↓␈α-␈α
e␈↓βi␈↓␈αlist;␈α␈↓αl␈↓␈α
will␈αbe
␈↓␈↓ β8bound␈α
to␈α
a␈α
generated␈α
symbol␈α
name␈α
;␈α
␈↓αm␈↓␈α
and␈α
␈↓αvpl␈↓␈α
are␈α
as␈α
always.

␈↓Fortified␈α
by␈α
the␈α
previous␈α
␈↓αcompile␈↓␈α
functions␈α
and␈α
this␈α
introduction␈α
the␈α
new␈α
␈↓αcompile␈↓␈α
should␈α
be␈α
clear.

␈↓αcompile <= λ[[fn;vars;exp]
␈↓α            prog[[n]
␈↓α               n ← length [vars];
␈↓α            return [append [list[list [LAP;fn;SUBR]];
␈↓α                           mkpush [n;1];
␈↓α                           compexp [exp; -n; prup [vars;1]]
␈↓α                           list [list [SUB;P;list [C;0;0;n;n]]]
␈↓α                           ((POPJ P)) ]]]

␈↓αprup <= λ[[vars;n]
␈↓α          [null [vars] → NIL;
␈↓α           T → cons [cons [car [vars]; n]; prup [cdr [vars];n+1]]]

␈↓αmkpush <= λ[[n;m]
␈↓α            [lessp [n;m] → NIL
␈↓α             T → cons [list [PUSH;P;cdr[assoc[m;aclist]]]; mkpush [n;m+1]]]
␈↓␈↓↓7.6␈↓ λ⊃A compiler for simple ␈↓αeval␈↓↓     151␈↓α


␈↓αloadac <= λ[[n;k]
␈↓α            [greaterp[n;0] → NIL;
␈↓α             T → cons[list[MOVE;cdr[assoc[k;aclist]];n;P];loadac[n+1;k+1]]]]


␈↓αcompexp <= λ[[exp;m;vpl]
␈↓α             [null [exp] → ((MOVEI AC1 0));
␈↓α              or [eq [exp;T]; numberp [exp]] → list [list [MOVEI;AC1;list [QUOTE; exp]]];
␈↓α              atom [exp] → list [list [MOVE;AC1;m + cdr [assoc [exp;vpl]]; P]];
␈↓α              eq [car [exp]; COND] → comcond [cdr [exp]; m; gensym [ ]; vpl];
␈↓α              eq [car [exp]; QUOTE] → list [list [MOVEI;AC1;exp]];
␈↓α              eq [atom [car [exp]] → λ[[n]append [complis [cdr [exp];m;vpl]
␈↓α                                                  loadac [1-n;1];
␈↓α       ␈↓π 16␈↓α
␈↓α                                                  list [list [SUB;P;list [C;0;0;n;n]]
␈↓α                                                     list [list [CALL;n;
␈↓α                                                             list [E; car [exp]]]]]
␈↓α                                     [length [cdr [exp]]]]]



␈↓αcomcond <= λ[[u;m;l;vpl]
␈↓α             [null [u] → list[l];
␈↓α              T → λ[[l1] append [comexp [caar [u];m;vpl];
␈↓α                                list [list[JUMPE;AC1;l]];
␈↓α     ␈↓π 17␈↓α
␈↓α                                compexp [cadar [u];m;vpl];
␈↓α                                list [list [JRST;l];l1]
␈↓α                                comcond [cdr [u];m;l;vpl]]]
␈↓α                gensym [ ]]



␈↓Here␈α
is␈α
a␈α
partial␈α
sketch␈α
of␈α
␈↓αcompile␈↓␈α
operating␈α
on␈α
␈↓αj␈α
<=␈α
λ[[x;y]f[g[x];h[y]]].␈↓






________________
␈↓α␈↓π 16␈↓α␈α
␈↓This␈α∞is␈α
sufficiently␈α
mysterious␈α∞to␈α
warrant␈α
explanation.␈α∞ The␈α
"␈↓αλ[[n] ... ][length[cdr[exp]]␈α
␈↓"␈α∞is␈α
an
␈↓internal␈α∂λ-expression␈α∂of␈α∞one␈α∂argument,␈α∂analogous␈α∂to␈α∞the␈α∂call␈α∂on␈α∞␈↓αcompapply␈↓␈α∂in␈α∂the␈α∂first␈α∞compiler
␈↓(page␈α
139).

␈↓α␈↓π 17␈↓α␈α
␈↓The␈α
same␈α
trickery␈α
used␈α
here␈α
as␈α
in␈α
␈↓αcompexp␈↓:␈α
␈↓αl1␈↓␈α
is␈α
bound␈α
to␈α
the␈α
value␈α
of␈α
␈↓αgensym[␈α
].␈↓
␈↓␈↓↓152  Compilation␈↓ 57.6␈↓


␈↓αcompile[J;(X Y);(F (G X) (H Y))] ␈↓gives:␈↓α
␈↓α␈↓ αXappend␈↓ βH[((LAP J SUBR));      (**1**)
␈↓α␈↓ αX␈↓ βH mkpush[2;1];
␈↓α␈↓ αX␈↓ βH compexp[(F (G X)(H Y));-2;prup[(X Y);1]];
␈↓α␈↓ αX␈↓ βH (SUB P (C 0 0 2 2))
␈↓α␈↓ αX␈↓ βH ((POPJ P ))];␈↓

␈↓where:
␈↓␈↓ β\␈↓αmkpush[2;1]␈↓ gives ␈↓α((PUSH P AC2)(PUSH P AC1)),␈↓ and
␈↓␈↓ ∧j␈↓αprup[(X Y);1]␈↓ gives ␈↓α((X . 1)(Y . 2))␈↓.

␈↓␈↓αcompexp[(F (G X)(H Y));-2;((X . 1)(Y . 2))]␈↓ results in:
␈↓α␈↓ αXappend␈↓ βH[complis[((G X)(H Y));-2;((X . 1)(Y . 2))];
␈↓α␈↓ αX␈↓ βHloadac[-1;1];
␈↓α␈↓ αX␈↓ βH((SUB P (C 0 0 2 2)));
␈↓α␈↓ αX␈↓ βH((CALL 2(E F)))]

␈↓α␈↓and ␈↓αloadac[-1;1]␈↓ evaluates to: ␈↓α((MOVE AC1 -1 P)(MOVE AC2 0 P))␈↓.

␈↓Thus (**1) above, is of the form:

␈↓α␈↓ αX␈↓ βH((LAP J SUBR)
␈↓α␈↓ αX␈↓ βH (PUSH P AC2)
␈↓α␈↓ αX␈↓ βH (PUSH P AC1)
␈↓α␈↓ αX␈↓ βH complis[((G X)(H Y));-2;((X . 1)(Y . 2))]
␈↓α␈↓ αX␈↓ βH (MOVE AC1 -1 P)
␈↓α␈↓ αX␈↓ βH (MOVE AC2 0 P)
␈↓α␈↓ αX␈↓ βH (CALL 2 ( E F))
␈↓α␈↓ αX␈↓ βH (SUB P (C 0 0 2 2))
␈↓α␈↓ αX␈↓ βH (POPJ P)
␈↓α␈↓ αX␈↓ βH)


␈↓αcomplis␈↓␈α
is␈α
interesting␈α
since␈α
it␈α
actually␈α
uses␈α
the␈α
␈↓αvpl␈↓␈α
we␈α
have␈α
been␈α
carrying␈α
along.␈α
It␈α
gives␈α
rise␈α
to:

␈↓α␈↓ αXappend␈↓ βH[compexp[(G X);-2;((X . 1)(Y . 2))];
␈↓α␈↓ αX␈↓ βH ((PUSH P AC1));
␈↓α␈↓ αX␈↓ βH complis[((H Y));-3;((X . 1)(Y . 2))]]

␈↓α␈↓and the ␈↓αcompexp␈↓ computation involves, in part:

␈↓α␈↓ αXappend[complis[(X);-2;((X . 1)(Y . 2))];
␈↓α␈↓ αX␈↓ βH ((MOVE AC1 0 P));
␈↓α␈↓ αX␈↓ βH ((SUB P (C 0 0 1 1));
␈↓α␈↓ αX␈↓ βH ((CALL 1 (E G))]
␈↓␈↓↓7.6␈↓ λ⊃A compiler for simple ␈↓αeval␈↓↓     153␈↓α


␈↓α␈↓Finally this ␈↓αcomplis␈↓ generates the long awaited variable reference using:

␈↓␈↓αcompexp[X;-2;((X . 1)(Y . 2))] ␈↓giving,
␈↓␈↓ αX␈↓ βH␈↓α((MOVE AC1 -1 P))␈↓.

␈↓Notice that the offset is different within the call:

␈↓␈↓ ∧h␈↓α complis[((H Y));-3;((X . 1)(Y . 2))].␈↓

␈↓Finally, you should convince yourself that the code, though inefficient, is correct.

␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓I␈α
Simple␈α
problems

␈↓1.␈α
Evaluate␈α
␈↓αcompile[␈↓␈α
h.s.]␈α
etc.

␈↓2.␈α
Complete␈α
the␈α
code␈α
generation␈α
for␈α
the␈α
above␈α
example.



␈↓␈↓ ∧Y␈↓↓7.7  A project: Efficient compilation␈↓


␈↓Certainly␈αthe␈αmost␈αstriking␈αfeature␈αof␈αthe␈αlast␈α␈↓αcompile␈↓␈αis␈αits␈αoutrageous␈αinefficiency.␈αExamination␈αof
␈↓the␈α
compilation␈α
of␈α
the␈α
most␈α
simple␈α
function␈α
suggests␈α
many␈α
possible␈α
improvements.

␈↓We␈α∂should␈α⊂first␈α∂clarify␈α⊂what␈α∂we␈α∂mean␈α⊂the␈α∂efficiency␈α⊂in␈α∂this␈α∂context.␈α⊂ We␈α∂might␈α⊂mean␈α∂minimal
␈↓compile-time.␈α⊃In␈α⊃this␈α⊂case␈α⊃we␈α⊃would␈α⊃want␈α⊂a␈α⊃very␈α⊃simple␈α⊃compiler;␈α⊂this␈α⊃usually␈α⊃means␈α⊃that␈α⊂the
␈↓complied␈αcode␈α
is␈αextraordinarily␈αbad.␈α
Such␈αcompilers␈α
might␈αsuffice␈αfor␈α
debugging␈αruns␈α
or␈αstudent
␈↓projects.␈α
 More␈α
likely,␈α
efficiency␈αcompilation␈α
is␈α
taken␈α
to␈α
mean␈αproduction␈α
of␈α
code␈α
which␈α
we␈αcould
␈↓expect␈αfrom␈αa␈αreasonably␈αbright␈αmachine-language␈αprogrammer.␈α It␈αshould␈αrun␈αreasonably␈αfast,␈αnot
␈↓have␈α⊂obviously␈α∂redundant␈α⊂instructions,␈α⊂and␈α∂not␈α⊂take␈α∂too␈α⊂much␈α⊂space␈α∂in␈α⊂the␈α∂machine.␈α⊂It␈α⊂is␈α∂this
␈↓second␈α
interpretation␈α
of␈α
efficiency␈α
which␈α
we␈α
shall␈α
use.

␈↓A␈α⊃major␈α⊃inefficiency␈α⊂occurs␈α⊃in␈α⊃saving␈α⊃and␈α⊂restoring␈α⊃quantities␈α⊃on␈α⊂the␈α⊃stack.␈α⊃For␈α⊃example,␈α⊂the
␈↓sequence␈α∞␈↓α(PUSH␈α∂P␈α∞AC1)(POP␈α∂P␈α∞AC1)␈↓␈α∂serves␈α∞no␈α∞useful␈α∂purpose.␈α∞This␈α∂is␈α∞a␈α∂symptom␈α∞of␈α∂a␈α∞more
␈↓serious␈α∞disease.␈α
The␈α∞compiler␈α
does␈α∞not␈α
remember␈α∞what␈α
will␈α∞be␈α
in␈α∞the␈α
ACs␈α∞at␈α
run-time.␈α∞Since␈α
the
␈↓arguments␈α⊃to␈α⊃a␈α⊃function␈α⊃call␈α⊃must␈α⊃be␈α⊃passed␈α⊃through␈α⊃the␈α⊃special␈α⊃AC␈α⊃registers␈α⊃and␈α⊃since␈α⊃it␈α⊃is
␈↓expensive␈αto␈αsave␈αand␈αrestore␈αthese␈αregisters,␈αwe␈αshould␈αmake␈αa␈αconcerted␈αeffort␈αto␈αremember␈αwhat
␈↓quantities␈α
are␈α∞in␈α
which␈α
ACs␈α∞and␈α
not␈α
reload␈α∞them␈α
unnecessarily.␈α
This␈α∞process␈α
will␈α∞certainly␈α
make
␈↓the␈αcompiler␈αmore␈αcomplicated␈αand␈αthat␈αwill␈αmean␈αlonger␈αcompilation␈αtime␈αbut␈αcompilation␈αusually
␈↓occurs␈α
less␈α
frequently␈α
than␈α
execution␈α
of␈α
compiled␈α
code.

␈↓Here␈α
are␈α
some␈α
possibilities.
␈↓␈↓↓154  Compilation␈↓ 57.7␈↓


␈↓****ADD␈α
'EM*****

␈↓diatribe␈αabout␈αgo␈αto␈αA␈αmajor␈αobstacle␈αto␈αthis␈αkind␈αof␈αoptimization␈αis␈αthe␈αunrestricted␈αuse␈αof␈αlabels
␈↓and␈α
gotos.␈α
Consider␈α
a␈αpiece␈α
of␈α
compiler␈α
code␈α
which␈αhas␈α
a␈α
label␈α
attached␈αto␈α
it.␈α
 Before␈α
we␈α
can␈αbe
␈↓assured␈α
of␈α
the␈αintegrity␈α
of␈α
an␈αAC␈α
we␈α
must␈α
ascertain␈αthat␈α
every␈α
possible␈αpath␈α
to␈α
that␈αlabel␈α
maintains
␈↓that␈α
AC.␈α
 This␈α
is␈α
a␈α∞very␈α
difficult␈α
task.␈α
The␈α
label␈α
and␈α∞goto␈α
structure␈α
required␈α
by␈α
␈↓αcompile␈↓␈α∞is␈α
quite
␈↓simple.␈αHowever␈αif␈αwe␈αwished␈αto␈αbuild␈αa␈αcompiler␈αfor␈αLISP␈αwirh␈α␈↓αprog␈↓s␈αwe␈αwould␈αhave␈αto␈αconfront
␈↓this␈α
problem.



␈↓␈↓ ∧j␈↓↓7.8  A project: One-pass assembler␈↓


␈↓III␈α
A␈α
one-pass␈α
assembler.

␈↓Write␈α∂a␈α∂one-pass␈α∂assembler␈α∂for␈α∂the␈α∂code␈α∂generated␈α∂by␈α∂the␈α∂␈↓αcompile␈↓␈α∂function␈α∂of␈α∂this␈α⊂section.␈α∂You
␈↓should␈α
be␈α
aware␈α
of␈α
the␈α
following␈α
points:

␈↓␈↓↓a.␈↓␈α␈↓αQUOTE␈↓d␈αexpressions␈αmust␈αbe␈αprotected␈α
from␈αgarbage␈αcollection.␈αThe␈αsimplest␈αway␈αto␈α
accomplish
␈↓␈↓ α_this␈α
it␈α
to␈α
save␈α
them␈α
on␈α
a␈α
list,␈α
say␈α
␈↓αQLIST␈↓.

␈↓␈↓↓b.␈↓␈α
Use␈α
the␈α
operation␈α
codes␈α
of␈α
Section␈α
7.5)␈α
****MORE␈α
ON␈α
INST␈α
FORMAT.***

␈↓␈↓↓c.␈↓␈α
Design␈α
a␈α
simple␈α
fixup␈α
scheme.␈α
 Notice␈α
that␈α
␈↓αcompile␈↓'s␈α
code␈α
will␈α
require␈α
address␈α
fixups␈α
at␈α
most.

␈↓␈↓↓d.␈↓␈αRecall␈αthat␈αthe␈αformat␈αof␈αthe␈αcode␈αis␈αa␈αlist.␈αThe␈αitems␈αin␈αthe␈αlist␈αare␈αeither␈αatomic␈α--␈αrepresenting
␈↓␈↓ α_labels␈α∃--,␈α∃or␈α∃lists␈α∃--␈α∃representing␈α∃instructions--.␈α∃ The␈α∃instructions␈α∃have␈α∃varying␈α∃format.
␈↓␈↓ α_Perhaps␈α
a␈α
table-driven␈α
scheme␈α
can␈α
be␈α
used.

␈↓␈↓↓e.␈↓␈α
Some␈α
attempt␈α
should␈α
be␈α
made␈α
to␈α
generate␈α
error␈α
messages.

␈↓␈↓↓f.␈↓␈αMany␈αof␈αthe␈αconstants,␈α
␈↓α(C␈α0␈α0␈α␈↓n␈αn␈↓α)␈↓,␈αoccur␈α
frequently;␈αthese␈αconstants␈αare␈αonly␈α
referenced,␈αnever
␈↓␈↓ α_changed␈αby␈αexecution␈α
of␈αthe␈αcompiled␈αcode.␈α
 Write␈αyour␈αassembler␈α
to␈αmaintain␈αonly␈αone␈α
copy
␈↓␈↓ α_of␈α
each.␈α
The␈α
constants␈α
should␈α
be␈α
stored␈α
directly␈α
after␈α
the␈α
compiled␈α
code.

␈↓␈↓↓f␈↓.␈α
 Try␈α
to␈α
be␈α
equally␈α
clever␈α
about␈α
storing␈α
␈↓αQUOTE␈↓d␈α
expressions.

␈↓IV␈α
***problem␈α
on␈α
internal␈α
lambdas
␈↓␈↓↓7.9␈↓ εiA project: Syntax directed compilation     155␈↓


␈↓␈↓ ∧,␈↓↓7.9  A project: Syntax directed compilation␈↓


␈↓compilation␈α
for␈α
sae

␈↓BNF␈α
for␈α
mexprs

␈↓syntax␈α
directed␈α
compiler

␈↓scanner␈α
parser



␈↓␈↓ ¬π␈↓↓7.10  A deep-binding compiler␈↓


␈↓**sketch␈α
of␈α
tgmoaf␈α
deep␈α
binder␈α
conventions

␈↓**project:␈α
do␈α
tgmoafr␈α
and␈α
eval␈α
d-b␈α
compr

␈↓**hints␈α
about␈α
globals␈α
and␈α
name␈α
stack

␈↓**project:␈α
do␈α
eval␈α
compiler␈α
using␈α
name-value␈α
stack␈α
for␈α
locals.



␈↓␈↓ ∧O␈↓↓7.11  Compilation and global variables␈↓


␈↓****␈α
expand␈α
greatly***

␈↓The␈α
models␈α
of␈α
compilation␈αwhich␈α
we␈α
have␈α
sketched␈αso␈α
far␈α
store␈α
their␈αλ-variables␈α
in␈α
the␈α
stack,␈α␈↓αP␈↓.
␈↓References␈αto␈αthose␈αvariables␈αin␈αthe␈αbody␈αof␈α
the␈αλ-expression␈αare␈αmade␈αto␈αthose␈αstack␈αentries.␈α
 This
␈↓scheme␈α∞suffices␈α∞only␈α∞for␈α∞lambda␈α∞or␈α∞␈↓αprog␈↓␈α
(local)␈α∞variables.␈α∞ We␈α∞have␈α∞said␈α∞that␈α∞λ-expressions␈α
may
␈↓refer␈αto␈αglobal␈αor␈αfree␈αvariables.␈α The␈αlookup␈αmechanism␈αsimply␈αfinds␈αthe␈αcurrent␈αbinding␈α
of␈αthat
␈↓global␈αin␈αthe␈αsymbol␈αtable.␈α Notice␈αthat␈αthis␈αis␈αa␈αdifferent␈αstrategy␈αthan␈αthe␈αglobal␈αlookup␈αof␈αAlgol.
␈↓In␈α
Algol,␈α
when␈α
a␈α
procedure␈α
refers␈α
to␈α
a␈α
global␈α
we␈α
take␈α
the␈α
binding␈α
which␈α
was␈α
current␈α
at␈α
the␈αpoint␈α
of
␈↓definition␈α
of␈α
the␈α
procedure.␈α
 The␈α
LISP␈αmechanism␈α
is␈α
called␈α
␈↓↓dynamic␈α
binding␈↓.␈α
 It␈α
corresponds␈αto
␈↓physically␈αsubstituting␈αthe␈αbody␈α
of␈αthe␈αdefinition␈αof␈α
the␈αfunction␈αwherever␈αit␈α
is␈αcalled.␈α Its␈αmodel␈α
of
␈↓implementation␈α
is␈α
simpler␈α
than␈α
that␈α
required␈α
for␈α
Algol.␈α
 We␈α
don't␈α
need␈α
the␈α
static␈α
chain␈α
for␈αthis␈α
case
␈↓of␈α⊂global␈α∂lookup.␈α⊂ Thus␈α∂interpreted␈α⊂expressions␈α∂encounter␈α⊂no␈α∂problems␈α⊂when␈α∂faced␈α⊂with␈α∂global
␈↓variables.␈α⊂ There␈α⊂are␈α⊂potential␈α⊂difficulties␈α∂for␈α⊂compiled␈α⊂code.␈α⊂ If␈α⊂all␈α∂we␈α⊂store␈α⊂of␈α⊂the␈α⊂stack␈α⊂in␈α∂a
␈↓compiled␈α⊂function␈α⊃is␈α⊂the␈α⊂value␈α⊃of␈α⊂a␈α⊂variable␈α⊃then␈α⊂another␈α⊂program␈α⊃which␈α⊂expects␈α⊂to␈α⊃use␈α⊂that
␈↓variable␈α
globally␈α
will␈α
have␈αno␈α
way␈α
of␈α
finding␈αthat␈α
stored␈α
value.␈α
 One␈αscheme␈α
is␈α
to␈α
store␈α
pairs␈αon
␈↓the␈α
stack:␈α
name␈α
and␈α
value␈α
(LISP 1.85)␈α
then␈α
we␈αcan␈α
search␈α
the␈α
stack␈α
for␈α
the␈α
latest␈α
binding.␈α It␈α
works.
␈↓␈↓↓156  Compilation␈↓ +7.11␈↓


␈↓With␈α∞this␈α∞scheme␈α∞we␈α∞can␈α∞dispense␈α∞with␈α∞the␈α∞␈↓αVALUE␈↓-cell.␈α∞ Actually␈α∞this␈α∞scheme␈α∞isn't␈α∞all␈α∞that␈α
bad.
␈↓The␈αcompiler␈αcan␈αstill␈α`know'␈αwhere␈αall␈αthe␈αlocal␈αvariables␈αare␈αon␈αthe␈αstack␈αand␈αcan␈αbe␈αa␈αbit␈αclever
␈↓about␈α⊃searching␈α⊃for␈α⊃the␈α⊃globals.␈α⊃ Notice␈α⊃this␈α⊃is␈α⊃the␈α⊃old␈α⊃symbol␈α⊃table␈α⊃mechanism␈α∩(a-list)␈α⊃again.
␈↓LISP 1.85␈α
was␈α
designed␈α
for␈α
a␈α
paging␈α
machine␈α
(XDS 940)␈α
and␈α
a␈α
few␈α
unpleasant␈α
features␈α∞crept␈α
in
␈↓because␈αof␈α
this.␈α (However,␈αon␈α
the␈αpositive␈α
side␈αsome␈αnice␈α
coding␈αtricks␈α
to␈αminimize␈αpage␈α
references
␈↓also␈α
arose.)

␈↓The␈α
solution␈α
proposed␈α
by␈αthe␈α
LISP␈α
1.6␈α
implementation␈αcalled␈α
␈↓↓shallow␈α
binding␈↓,␈α
allows␈αthe␈α
compiled
␈↓code␈α∞to␈α∞directly␈α∞access␈α∞the␈α∞␈↓αVALUE␈↓-cell␈α∞in␈α∞the␈α∞symbol␈α∞table.␈α∞ There␈α∞is␈α∞an␈α∞artifact␈α∞in␈α∞the␈α∞compiler
␈↓(and␈α∂assembler)␈α∂called␈α∂␈↓αSPECIAL␈↓.␈α∂ Variables␈α∂which␈α⊂are␈α∂to␈α∂be␈α∂used␈α∂globally␈α∂are␈α⊂declared␈α∂special
␈↓variables.␈α∞ When␈α∞a␈α∞variable,␈α∞say␈α∞␈↓αx␈↓,␈α∞is␈α∞declared␈α∞special␈α∞the␈α∞compiler␈α∞will␈α∞emit␈α∞a␈α∞reference␈α∞to␈α∞␈↓αx␈↓␈α
as
␈↓␈↓α(MOVE AC␈↓βi␈↓α (SPECIAL X))␈↓␈α~or␈α~␈↓α(MOVEM AC␈↓βi␈↓α (SPECIAL X))␈↓␈α~rather␈α~than␈α~the␈α→corresponding
␈↓reference␈αto␈αa␈αlocation␈αon␈αthe␈αstack.␈α When␈α
the␈αLISP␈αassembler␈αsees␈αthe␈αindicator␈α␈↓αSPECIAL␈↓,␈αit␈α
will
␈↓search␈α
the␈αsymbol␈α
table␈αfor␈α
the␈α
␈↓αVALUE␈↓-cell␈αof␈α
␈↓αX␈↓␈αand␈α
assemble␈α
a␈αreference␈α
to␈αthat␈α
cell.␈α
 Since␈αthe
␈↓location␈α
of␈α
the␈α∞value␈α
cell␈α
does␈α∞␈↓↓not␈↓␈α
change,␈α
we␈α
can␈α∞always␈α
find␈α
the␈α∞current␈α
binding␈α
posted␈α∞in␈α
the
␈↓␈↓αcdr␈↓-part␈α
of␈α
that␈α
cell.␈α
 Notice␈α
too␈α
that␈α∞any␈α
interpreted␈α
function␈α
can␈α
also␈α
sample␈α
the␈α∞␈↓αVALUE␈↓-cell␈α
so
␈↓global␈α⊃values␈α⊃can␈α⊃be␈α⊂passed␈α⊃between␈α⊃compiled␈α⊃and␈α⊂interpreted␈α⊃functions.␈α⊃ The␈α⊃values␈α⊃of␈α⊂local
␈↓variables␈αare␈αstill␈αposted␈αon␈αthe␈αstack.␈α This␈αthen␈αis␈αthe␈αreason␈αfor␈αdepressing␈αthe␈αactual␈αvalue␈αone
␈↓level.

␈↓****pic







␈↓We␈α∂have␈α∂not␈α∂yet␈α∂discussed␈α∞the␈α∂mechanism␈α∂which␈α∂will␈α∂allow␈α∞us␈α∂to␈α∂pass␈α∂back␈α∂and␈α∂forth␈α∞between
␈↓compiled␈α∪and␈α∪interpreted␈α∪functions.␈α∪ To␈α∪complete␈α∩that␈α∪discussion␈α∪we␈α∪must␈α∪introduce␈α∪the␈α∩␈↓	SM␈↓
␈↓instruction␈α
for␈α
calling␈α
a␈α
function:

␈↓PUSHJ P fn␈↓ βh␈↓αC␈↓(P) ← ␈↓	PC␈↓
␈↓␈↓ βhP ← ␈↓αC␈↓(P) + 1
␈↓␈↓ βh␈↓	PC␈↓ ← fn. This is called the "push-jump" instruction. Exit with POPJ.

␈↓First␈α
we␈α
require␈α
that␈α
the␈α
calling␈α
conventions␈α
for␈α
both␈α
kinds␈α
of␈α
functions␈α
be␈α
isomorphic.

␈↓When␈α∩an␈α∩interpreted␈α⊃function␈α∩calls␈α∩a␈α∩compiled␈α⊃(or␈α∩primitive)␈α∩function,␈α⊃␈↓αeval␈↓␈α∩will␈α∩look␈α∩for␈α⊃the
␈↓indicator,␈α∞␈↓αSUBR␈↓;␈α∞then␈α∂retrieve␈α∞the␈α∞machine␈α∞address␈α∂of␈α∞the␈α∞code␈α∞and␈α∂enter␈α∞via␈α∞a␈α∂␈↓αPUSHJ␈↓.␈α∞That
␈↓code␈αshould␈α
exit␈α(back␈αto␈α
␈↓αeval␈↓)␈αvia␈α
a␈α␈↓αPOPJ␈↓,␈αafter␈α
assuring␈αthat␈α
the␈αstack,␈αP,␈α
has␈αbeen␈α
restored␈αto
␈↓the␈α
state␈α
at␈α
the␈α
time␈α
of␈α
entry.

␈↓Compiled␈α∂functions␈α∞call␈α∂other␈α∞functions␈α∂via␈α∂the␈α∞␈↓α(CALL ␈↓n␈↓α (E ␈↓fn␈↓α))␈↓␈α∂artifact.␈α∞ The␈α∂␈↓αCALL␈↓␈α∂opcode␈α∞is
␈↓actually␈αan␈αillegal␈αinstruction␈αwhich␈αis␈αtrapped␈αto␈αa␈αsubmonitor␈αinside␈α␈↓αeval␈↓.␈α This␈αsubmonitor␈αlooks
␈↓␈↓↓7.11␈↓ π9Compilation and global variables     157␈↓


␈↓down␈αthe␈αproperty␈αlist␈αof␈αthe␈αatom,␈αfn,␈αfor␈αa␈αfunction␈αindicator␈α(␈↓αSUBR,␈αEXPR␈↓,␈αetc).␈α The␈αfunction
␈↓is␈α∞called␈α∞and␈α∞control␈α∞is␈α∞then␈α∞returned␈α∂to␈α∞the␈α∞address␈α∞immediately␈α∞following␈α∞the␈α∞␈↓αCALL␈↓.␈α∂ In␈α∞many
␈↓cases␈α
this␈α
␈↓αCALL␈↓␈α
can␈α
be␈α
replaced␈α
by␈α
a␈α
␈↓α(PUSHJ P ␈↓fn␈↓α)␈↓,␈α
but␈α
not␈α
always␈α
as␈α
we␈α
will␈α
see␈α
next.



␈↓␈↓ ¬⊗␈↓↓7.12  Functional Arguments␈↓


␈↓***more␈α
h.s.***

␈↓***farting␈α
with␈α
funarg***

␈↓funarg␈α
b-w.␈α
and␈α
weizen

␈↓**add␈α
deep-binding␈α
compiler**

␈↓what␈α
does␈α
this␈αsay␈α
about␈α
the␈α
CALL␈αmechanism␈α
in␈α
the␈α
compiler?␈α It␈α
says␈α
that␈α
the␈αcalling␈α
mechanism
␈↓for␈α
a␈α
functional␈α
argument␈α
must␈α
always␈αbe␈α
trapped␈α
the␈α
submonitor␈α
and␈α
decoded.␈α
 We␈αcannot␈α
replace
␈↓that␈α∂call␈α⊂with␈α∂a␈α⊂PUSHJ␈α∂to␈α⊂some␈α∂machine␈α⊂language␈α∂code␈α⊂for␈α∂the␈α⊂function␈α∂because␈α⊂the␈α∂function
␈↓referred␈α
to␈α
can␈α
change.␈α We␈α
actually␈α
use␈α
a␈αCALLF␈α
instruction␈α
to␈α
designate␈αa␈α
call␈α
on␈α
a␈αfunctional
␈↓argument.



␈↓␈↓ ∧T␈↓↓7.13  Tracing and debugging in LISP␈↓


␈↓When␈α
can␈α∞the␈α
␈↓αCALL␈↓␈α
instruction␈α∞be␈α
replaced␈α
by␈α∞a␈α
␈↓αPUSHJ␈↓?␈α
 The␈α∞␈↓αPUSHJ␈↓␈α
instruction␈α
is␈α∞used␈α
to
␈↓call␈α⊂a␈α⊂machine␈α⊂language␈α⊂function.␈α⊂ Obviously␈α⊂if␈α⊂we␈α⊂are␈α⊂calling␈α⊂an␈α⊂interpreted␈α⊂function,␈α⊂(it␈α∂has
␈↓indicator␈α␈↓αEXPR␈↓␈αof␈α␈↓αFEXPR␈↓)␈α␈↓αPUSHJ␈↓␈αis␈αthe␈αwrong␈αthing␈αto␈αdo.␈α In␈αthis␈αcase␈αwe␈αmust␈αpass␈αcontrol
␈↓to␈α␈↓αeval␈↓,␈αevaluate␈αthe␈αfunction␈αwith␈αthe␈αappropriate␈αarguments,␈αreturn␈αthe␈αvalue␈αin␈α␈↓αAC1␈↓␈αand␈αfinally
␈↓return␈α∞control␈α∂to␈α∞the␈α∂instruction␈α∞following␈α∂the␈α∞␈↓αCALL␈↓.␈α∂ If␈α∞the␈α∂function␈α∞being␈α∂called␈α∞is␈α∂a␈α∞machine
␈↓language␈αroutine␈α(indicator␈αis␈α␈↓αSUBR␈↓␈αor␈α␈↓αFSUBR␈↓)␈αthen␈αwe␈αcould␈αreplace␈αthe␈α␈↓αCALL␈↓␈αwith␈αa␈α
␈↓αPUSHJ␈↓.
␈↓This␈αwould␈α`short-circuit'␈αthe␈αcall␈αon␈αthe␈αsubmonitor␈αand␈αthe␈αcalling␈αof␈αthe␈αfunction␈αcould␈αbe␈αdone
␈↓more␈α⊃quickly.␈α⊂ There␈α⊃are␈α⊃many␈α⊂occasions␈α⊃in␈α⊂which␈α⊃we␈α⊃do␈α⊂not␈α⊃wish␈α⊂to␈α⊃make␈α⊃this␈α⊂replacement,
␈↓however.

␈↓Assume␈α∂for␈α∂the␈α∂moment␈α∂that␈α∂I␈α∂am␈α∂mortal␈α∂and␈α∂that␈α∂my␈α∂LISP␈α∂program␈α∂has␈α∂some␈α∂bugs.␈α∂ Crucial
␈↓pieces␈α
of␈α
information␈α
about␈α
the␈α
behavior␈α∞of␈α
the␈α
program␈α
are:␈α
which␈α
functions␈α
are␈α∞being␈α
entered,
␈↓what␈αare␈αthe␈αactual␈αparameters,␈αand␈αwhat␈α
are␈αthe␈αvalues␈αbeing␈αreturned.␈α Assume␈αthat␈αwe␈α
wish␈αto
␈↓monitor␈α
the␈α
behavior␈α∞of␈α
the␈α
function,␈α
␈↓αFOO␈↓.␈α∞We␈α
will␈α
hide␈α
the␈α∞real␈α
definition␈α
of␈α
␈↓αFOO␈↓␈α∞on␈α
another
␈↓symbol␈α
table␈α
entry␈α
(using␈α
␈↓αgensym[]␈↓)␈α
and␈α
redefine␈α
␈↓αFOO␈↓␈α
such␈α
that,␈α
when␈α
it␈α
is␈α
called,␈α
it␈α
will:

␈↓␈↓ αh␈↓↓1.␈↓␈α
print␈α
the␈α
values␈α
of␈α
the␈α
current␈α
actual␈α
parameters.
␈↓␈↓↓158  Compilation␈↓ )7.13␈↓


␈↓␈↓ αh␈↓↓2.␈↓␈α
use␈α
␈↓αapply␈↓␈α
to␈α
call␈α
the␈α
real␈α
defintion␈α
of␈α
␈↓αFOO␈↓␈α
with␈α
the␈α
current␈α
parameters.

␈↓␈↓ αh␈↓↓3.␈↓␈α
print␈α
the␈α
value␈α
of␈α
the␈α
call␈α
on␈α
␈↓αFOO␈↓.

␈↓␈↓ αh␈↓↓4.␈↓␈α
return␈α
control␈α
to␈α
the␈α
calling␈α
program.
␈↓This␈α
device␈α
is␈α
called␈α
tracing.

␈↓Since␈α␈↓αFOO␈↓␈αmay␈α
be␈αrecursive␈αwe␈α
should␈αalso␈αgive␈α
some␈αindication␈αof␈α
the␈αdepth␈αof␈α
recursion␈αbeing
␈↓executed.␈α
 Now␈α
every␈α
call␈α
on␈α
␈↓αFOO␈↓␈α
will␈α
give␈α
us␈α
the␈α
pertinent␈α
statistics.␈α
 Interpreted␈α
calls␈α
on␈α␈↓αFOO␈↓␈α
will
␈↓go␈α
through␈α
␈↓αeval␈↓,␈αand␈α
if␈α
␈↓α(CALL␈α
...␈αFOO)␈↓␈α
is␈α
being␈αused␈α
in␈α
the␈α
compiled␈αcode␈α
the␈α
submonitor␈αcan␈α
pass
␈↓control␈α∞to␈α∞the␈α∂tracing␈α∞mechanism;␈α∞but␈α∂if␈α∞the␈α∞␈↓αCALL␈↓␈α∂is␈α∞replaced␈α∞by␈α∂a␈α∞␈↓αPUSHJ␈↓,␈α∞the␈α∂control␈α∞passes
␈↓directly␈α
to␈α
the␈α
machine␈α
language␈α
code␈α
for␈α
␈↓αFOO␈↓␈α
and␈α
we␈α
cannot␈α
intercept␈α
the␈α
call.

␈↓On␈α⊃most␈α⊃implementations␈α⊃of␈α⊃LISP␈α⊃the␈α⊂␈↓αPUSHJ-CALL␈↓␈α⊃mechanism␈α⊃is␈α⊃under␈α⊃the␈α⊃control␈α⊃of␈α⊂the
␈↓programmer.␈α After␈αthe␈αprogram␈αis␈αsufficiently␈αdebugged,␈αreplace␈αthe␈α␈↓αCALL␈↓␈αwith␈αthe␈α␈↓αPUSHJ␈↓␈αand
␈↓the␈αprograms␈αwill␈αexecute␈αfaster.␈α But␈αbe␈αwarned␈αthat␈αthis␈αaction␈αis␈αirreversible␈αon␈αmost␈αmachines;
␈↓once␈α
the␈α
␈↓αCALL␈↓s␈α
have␈α
been␈α
overwritten␈α
it's␈α
tough␈α
beans!!

␈↓A␈α∞variant␈α
of␈α∞this␈α∞tracing␈α
scheme␈α∞can␈α∞be␈α
used␈α∞to␈α
monitor␈α∞␈↓αSET␈↓s␈α∞and␈α
␈↓αSETQ␈↓s␈α∞in␈α∞interpreted␈α
␈↓αprog␈↓s.
␈↓Since␈αcalls␈αon␈α␈↓αSET␈↓␈αand␈α␈↓αSETQ␈↓␈αare␈αinterpreted␈α(by␈α␈↓αeval␈↓␈αand␈αCo.),␈αwe␈αcan␈αmodify␈αtheir␈αdefinitions␈α
to
␈↓print␈α
the␈αname␈α
of␈αthe␈α
variable␈αand␈α
the␈αnew␈α
assignment,␈αdo␈α
it,␈αand␈α
return.␈α (question:␈α
can␈α
you␈αsee
␈↓why␈α
this␈α
perhaps␈α
won't␈α
(or␈α
shouldn't)␈α
work␈α
for␈α
compiled␈α
code?)

␈↓This␈αis␈αa␈αvery␈αbrief␈αdescription␈αof␈αdebugging␈α
in␈αLISP.␈α It␈αagain␈αshows␈αsome␈αof␈αthe␈αpower␈α
resultant
␈↓from␈α∞having␈α∞an␈α∞evaluator␈α∞available␈α∂at␈α∞runtime.␈α∞ Much␈α∞more␈α∞sophisticated␈α∂debugging␈α∞techniques
␈↓can␈α∃be␈α∃implemented,␈α∃particularly␈α∃in␈α⊗an␈α∃on-line␈α∃implementation␈α∃of␈α∃LISP.␈α∃Perhaps␈α⊗the␈α∃most
␈↓comprehensive␈α∞on-line␈α∞LISP␈α∞system␈α∞is␈α∞INTERLISP,␈α
an␈α∞outgrowth␈α∞of␈α∞BBN␈α∞LISP.␈α∞The␈α∞details␈α
of
␈↓this␈α
undertaking␈α
would␈α
take␈α
us␈α
too␈α
far␈α
afield␈α
from␈α
our␈α
current␈α
discussion.



␈↓␈↓ ¬␈↓↓7.14  Macros and special forms␈↓


␈↓Recall␈αour␈αdiscussion␈α
of␈αmacros␈αand␈α
special␈αforms␈αin␈α
Section␈α6.13.␈α We␈α
wish␈αto␈αextend␈αour␈α
compiler
␈↓to␈α
handle␈α
such␈α
definitions.

␈↓Consider␈α
the␈αexample␈α
of␈αdefining␈α
␈↓αplus␈↓␈αof␈α
an␈αindefinite␈α
number␈αof␈α
arguments␈αgiven␈α
on␈α
page␈α120.
␈↓Notice␈α∀first␈α∀that␈α∀difference␈α∀in␈α∀efficiency␈α∃between␈α∀the␈α∀interpreted␈α∀macro␈α∀(page␈α∀120)␈α∃and␈α∀the
␈↓interpreted␈αspecial␈αform␈α
(page␈α120)␈αis␈α
very␈αslight.␈α Both␈α
require␈αcalls␈αon␈α
␈↓αeval␈↓.␈α One␈αrequires␈α
explicit
␈↓user␈αcalls␈αon␈αthe␈αevaluator;␈αone␈αdoes␈αit␈αwithin␈αthe␈αevaluator.␈α In␈αthe␈αpresence␈αof␈αa␈αcompiler␈αwe␈αcan
␈↓frequently␈αmake␈αexecution␈α
of␈αmacros␈αmore␈αefficient␈α
than␈αtheir␈αspecial␈α
form␈αcounterpart.␈αThis␈αis␈α
the
␈↓case␈α
with␈α
␈↓αplus␈↓.␈α
 When␈α
␈↓αplus␈↓␈α
is␈α
called␈α
we␈α
know␈α
the␈α
number␈α
of␈α
arguments,␈α
and␈α
can␈α
simply␈α
expand
␈↓the␈α
macro␈α
to␈α
a␈α
nest␈α
of␈α
calls␈α
on␈α
␈↓α*plus␈↓.␈α
For␈α
example:

␈↓α␈↓ βjplus[4;add1[2];4] ␈↓expands to␈↓α *plus[4;*plus[add1[2];4]] ␈↓
␈↓␈↓↓7.14␈↓ λ Macros and special forms     159␈↓


␈↓which can be efficiently compiled.

␈↓There␈α
is␈α
a␈α
closely␈α
related␈α
use␈α
of␈α
LISP␈α
macros␈α
which␈α
is␈α
worth␈α
mentioning.␈α
 Recall␈α
on␈α
page␈α∞33␈α
we
␈↓defined␈α
␈↓αcoef␈↓␈α
as␈α
␈↓αcar␈↓.␈α
Compiled␈α
calls␈αon␈α
␈↓αcoef␈↓␈α
would␈α
invoke␈α
the␈α
function-calling␈α
mechanism,␈αwhereas
␈↓many␈α⊃compilers␈α⊃can␈α⊃substitute␈α⊃actual␈α⊃hardware␈α∩instructions␈α⊃for␈α⊃calls␈α⊃on␈α⊃␈↓αcar␈↓,␈α⊃resulting␈α∩in␈α⊃more
␈↓efficient␈α
run-time␈α
code.␈α
 So␈α
for␈α
efficiency's␈α
sake␈α
it␈αwould␈α
be␈α
better␈α
to␈α
write␈α
␈↓αcar␈↓␈α
instead␈α
of␈α␈↓αcoef␈↓.␈α
There
␈↓are␈αtwo␈αobjections␈αto␈αthis.␈αFirst,␈α␈↓αcoef␈↓␈αhas␈αmore␈αmnemonic␈αsignificance␈αthe␈α␈↓αcar␈↓.␈αSecond,␈αusing␈α␈↓αcar␈↓␈αwe
␈↓have␈α∞explicitly␈α∞tied␈α∞our␈α∞algorithm␈α∞to␈α∞the␈α∞representation.␈α∞Both␈α∞are␈α∞strong␈α∞objections.␈α∞ Macros␈α∞can
␈↓help␈α
overcome␈α
both␈α
objections.␈α
Define:

␈↓␈↓ ¬	␈↓αcoef <␈↓βm␈↓α= λ[[l]cons[CAR;cdr[l]]]␈↓. 

␈↓(Recall␈αthat␈αthe␈αwhole␈αcall␈α␈↓α(COEF␈α...␈α)␈↓␈αgets␈αbound␈αto␈α␈↓αl␈↓.)␈αSo␈αthe␈αuser␈αwrites␈α␈↓α(COEF␈α...)␈↓;␈αthe␈αevaluator
␈↓sees␈α␈↓α(COEF␈α...)␈↓␈αand␈αfinally␈αevaluates␈α␈↓α(CAR␈α...)␈↓;␈αand␈αthe␈αcompiler␈αsees␈α␈↓α(COEF␈α...)␈↓␈αand␈αcompiles␈αcode
␈↓for␈α␈↓α(CAR␈α...)␈↓.␈αSo␈αwe␈αcan␈αget␈αthe␈αefficient␈αcode,␈αthe␈αreadibility,␈αand␈αflexibility␈αof␈αrepresentation␈αwith
␈↓macros.

␈↓Since␈α␈↓αeval␈↓␈αhandles␈αcalls␈αon␈αspecial␈αforms,␈αwe␈αshould␈αexamine␈αthe␈αextensions␈αto␈α␈↓αcompile␈↓␈α
to␈αgenerate
␈↓such␈αcode.␈αWe␈αhave␈α
seen␈αthat␈αin␈αcompiling␈αarguments␈α
to␈α(normal)␈αfunctions,␈αwe␈αgenerate␈α
the␈αcode
␈↓for␈α
each,␈αfollowed␈α
by␈αcode␈α
to␈αsave␈α
the␈αresult␈α
in␈αthe␈α
run-time␈αstack,␈α
␈↓αP␈↓.␈α The␈α
argument␈αto␈α
a␈αspecial
␈↓form␈α∞is␈α∂␈↓↓unevaluated␈↓,␈α∞by␈α∂definition.␈α∞All␈α∞we␈α∂can␈α∞thus␈α∂do␈α∞for␈α∞a␈α∂call␈α∞of␈α∂the␈α∞form␈α∞␈↓αf[l]␈↓,␈α∂where␈α∞␈↓αf␈↓␈α∂is␈α∞a
␈↓special␈α
form,␈α
is␈α
pass␈α
the␈α
argument,␈α
compiling␈α
something␈α
like:

␈↓α␈↓ ¬≤(MOVEI AC1 (QUOTE (l)))
␈↓α␈↓ ¬h(CALL 1 (E F))

␈↓This␈α
should␈α
also␈α
be␈α
clear␈α
from␈α
the␈α
structure␈α
of␈α
␈↓αFEXPR␈↓␈α
calling␈α
in␈α
the␈α
␈↓	SM␈↓␈α
evaluator.

␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓I.␈α
Extend␈α
the␈α
last␈α
␈↓αcompile␈↓␈α
function␈α
to␈α
handle␈α
macros.

␈↓II.␈α⊂ We␈α⊂have␈α⊃seen␈α⊂the␈α⊂(necessarily)␈α⊃inefficient␈α⊂code␈α⊂generated␈α⊂by␈α⊃a␈α⊂compiler␈α⊂for␈α⊃␈↓αFEXPR␈↓␈α⊂calls.
␈↓Assume␈α⊂␈↓αand␈↓␈α⊂is␈α⊂a␈α⊂special␈α⊂form␈α⊂with␈α⊂an␈α⊂indefinite␈α⊂number␈α⊂of␈α⊂arguments.␈α⊂ Show␈α⊂how␈α⊃to␈α⊂modify
␈↓␈↓αcompile␈↓␈α
to␈α
recognize␈α
␈↓αand␈↓␈α
and␈α
compile␈α
efficient␈α
code␈α
for␈α
its␈α
execution.



␈↓␈↓ ¬q␈↓↓7.15  Numbers␈↓


␈↓In␈α
most␈α
implementations␈α
of␈αLISP,␈α
numbers␈α
are␈α
stored␈αas␈α
very␈α
simple␈α
kinds␈αof␈α
atoms:␈α
they␈α
do␈αnot
␈↓need␈α
print␈αnames,␈α
but␈αsince␈α
we␈αshould␈α
probably␈αallow␈α
fixed␈αand␈α
floating␈αpoint␈α
representation,␈αwe
␈↓do␈α
need␈α
indicators␈α
for␈α
these␈α
properties.␈α
 Thus:
␈↓␈↓↓160  Compilation␈↓ (7.15␈↓


␈↓␈↓ αXfixed-point 1␈↓ πλfloating-point 1.0
␈↓␈↓ αX|␈↓ πλ|
␈↓␈↓ αX|--→␈↓εα~~]␈↓--→␈↓ε[␈↓
FIXNUM␈↓ε]~~]␈↓--→␈↓ε[~␈↓1␈↓ε~]␈↓ πλ␈↓|--→␈↓εα~~]␈↓--→␈↓ε[␈↓
FLONUM␈↓ε]~~]␈↓--→␈↓ε[␈↓201400000000␈↓ε]


␈↓Notice␈α
that␈α
each␈α
actual␈α
number␈α
is␈α
stored␈αin␈α
FWS.␈α
 This␈α
representation␈α
is␈α
a␈α
bit␈α
expensive.␈α On␈α
some
␈↓machines␈αwe␈αcan␈αuse␈αa␈αcoding␈αtrick␈α
to␈αimprove␈αrepresentation␈αof␈αsome␈αnumbers.␈α Assume␈α
that␈αthe
␈↓addressing␈αspace␈α
of␈αthe␈α
machine␈αis␈α
2␈↓π18␈↓␈αand␈α
that␈αthe␈α
usual␈αsize␈α
of␈αa␈α
LISP␈αcore␈α
image␈αis␈α
significantly
␈↓smaller,␈αsay,␈α
N.␈α Then␈α
all␈αmemory␈α
address␈αreferences␈αgreater␈α
than␈αN␈α
are␈αillegal␈α
(and␈αtrapped␈αby␈α
the
␈↓monitor).␈α What␈αwe␈αwill␈αdo␈α
is␈αuse␈αthese␈αillegal␈αaddresses␈αto␈α
encode␈αsome␈αof␈αthe␈αsmaller␈αpositive␈α
and
␈↓negative␈α
integers,␈α
mapping␈α
zero␈α
on␈α
the␈α
middle␈α
address,␈α
the␈α
positive␈α
numbers␈α
to␈α
lower␈αaddresses␈α
and
␈↓the␈α∞negatives␈α
onto␈α∞the␈α
higher␈α∞addresses.␈α
 Thus␈α∞these␈α
smaller␈α∞integers␈α
are␈α∞represented␈α∞by␈α
pointers
␈↓outside␈α∂of␈α⊂the␈α∂normal␈α∂LISP␈α⊂addressing␈α∂space.␈α⊂ This␈α∂trick␈α∂can␈α⊂considerably␈α∂decrease␈α⊂the␈α∂storage
␈↓requirements␈α
for␈α
jobs␈α
heavily␈α
using␈α
small␈α
numbers.␈α
 (Numbers␈α
are␈α
not␈α
usually␈α
stored␈α
uniquely).





















␈↓␈↓↓␈↓ ¬0Picture of INUM Space␈↓ 

␈↓Most␈α
numerically␈αoriented␈α
programs␈αare␈α
faced␈αat␈α
some␈αtime␈α
with␈αoverflow.␈α
 That␈αis,␈α
they␈αattempt␈α
to
␈↓construct␈αa␈α
number␈αwhich␈α
is␈αtoo␈α
large␈αto␈αbe␈α
represented␈αin␈α
one␈αmachine␈α
location.␈α There␈α
are␈αnow
␈↓several␈αversions␈αof␈αLISP␈αwhich␈αwill␈α
automatically␈αchange␈αrepresentation␈αwhen␈αfaced␈αwith␈α
overflow.
␈↓This␈α
new␈α
representation␈α
called␈α
Bignums,␈α
could␈α
have␈α
the␈α
following␈α
structure:
␈↓␈↓↓7.15␈↓ 
Numbers     161␈↓










␈↓␈↓ ¬-␈↓↓Structure of a BIGNUM␈↓ 

␈↓The␈α
value␈α
of␈α
Bignum␈α
is␈α
given␈α
by:

␈↓where␈αα-1␈αis␈αthe␈αlargest␈αnumber␈α
representable␈αin␈αone␈αmachine␈αword.␈α The␈αintertranslations␈α
between
␈↓Inums,␈α
Fixnums␈α
or␈α
Flonums,␈α
and␈α
Bignums␈α
is␈α
done␈α
automatically␈α
by␈α
␈↓αeval␈↓␈α
and␈α
Co.
␈↓␈↓↓162  Storage structures and efficiency␈↓ B8.␈↓


␈↓␈↓ ¬}␈↓↓SECTION 8

␈↓↓␈↓ ∧
STORAGE STRUCTURES AND EFFICIENCY␈↓




␈↓Though␈αit␈αis␈αtrue␈αthat␈αany␈αalgorithm␈αcan␈αbe␈αcoded␈αin␈αterms␈αof␈αmanipulations␈αof␈αbinary␈αtrees,␈αthere
␈↓are␈α
many␈α
instances␈α
in␈α
which␈α
more␈α
efficient␈α
organizations␈α
of␈α
data␈α
exist.

␈↓At␈α
the␈α
most␈αelementary␈α
level␈α
are␈α
vectors␈αand␈α
arrays.␈α
 These␈α
data␈αstructures␈α
could␈α
certainly␈αbe␈α
stored
␈↓in␈α∞a␈α
list␈α∞structure␈α
format␈α∞and␈α
individual␈α∞components␈α
accessed␈α∞via␈α
␈↓αcar-cdr␈↓␈α∞chains.␈α∞ However,␈α
most
␈↓machines␈α∂have␈α∂a␈α⊂hardware␈α∂organization␈α∂which␈α∂can␈α⊂be␈α∂exploited␈α∂to␈α∂increase␈α⊂accessing␈α∂efficiency
␈↓over␈αthe␈αlist␈αrepresentation.␈α Similarly,␈αstrings␈αcan␈αbe␈αrepresented␈αas␈αlists␈αof␈αcharacters.␈α The␈αstring
␈↓processing␈α
operations␈α∞are␈α
expressible␈α∞as␈α
LISP␈α∞algorithms.␈α
 But␈α
again␈α∞this␈α
is␈α∞usually␈α
not␈α∞the␈α
most
␈↓resonable␈α
representation.␈α
Even␈αat␈α
the␈α
level␈αof␈α
list-structure␈α
operations,␈αsimple␈α
binary␈α
trees␈αmight␈α
not
␈↓be␈α
the␈α
most␈α
expeditious␈α
representation␈α
for␈α∞every␈α
problem.␈α
 Also␈α
many␈α
of␈α
the␈α
algorithms␈α∞we␈α
have
␈↓presented␈α∞in␈α∞LISP␈α∞are␈α∂overly␈α∞wasteful␈α∞of␈α∞computation␈α∞time.␈α∂ This␈α∞section␈α∞of␈α∞notes␈α∞will␈α∂begin␈α∞an
␈↓examination␈αof␈αalternatives␈αto␈αLISP␈αorganization.␈α There␈αis␈αno␈αbest␈αdata␈αrepresentation,␈αor␈αno␈αbest
␈↓algorithm.␈α
 The␈αrepresentations␈α
you␈α
choose␈αmust␈α
match␈αyour␈α
machine␈α
and␈αthe␈α
problem␈αdomain␈α
you
␈↓are␈α
studying.␈α∞ If␈α
your␈α
application␈α∞is␈α
strictly␈α
numerical␈α∞then␈α
list-structure␈α
is␈α∞not␈α
the␈α
answer;␈α∞if␈α
you
␈↓wish␈αto␈α
manipulate␈αsimple␈αlinear␈α
strings␈αof␈α
characters␈αthen␈αlist␈α
processing␈αis␈α
too␈αgeneral.␈α And␈α
there
␈↓are␈αmany␈α
applications␈αof␈αlist␈α
processing␈αwhich␈αare␈α
sufficiently␈αwell-behaved␈αthat␈α
you␈αdon't␈α
need␈αa
␈↓storage␈αallocation␈αdevice␈αas␈αcomplex␈αas␈αa␈αgarbage␈αcollector.␈α The␈αpoint␈αis␈αthat␈αif␈αyou␈αhave␈αseen␈αthe
␈↓list-processing␈α∞techniques␈α
in␈α∞a␈α∞rich␈α
environment␈α∞such␈α∞as␈α
LISP␈α∞and␈α∞have␈α
seen␈α∞the␈α∞applications␈α
to
␈↓which␈αLISP␈αmay␈αbe␈αput,␈αthen␈αyou␈αwill␈α
be␈αprepared␈αto␈αapply␈αthese␈αtechniques␈αin␈αa␈αmeaningful␈α
way.
␈↓Many␈α∀times␈α∀an␈α∀(inefficient)␈α∀representation␈α∀in␈α∀LISP␈α∀is␈α∀all␈α∀that␈α∀is␈α∀needed.␈α∀ You␈α∀get␈α∀a␈α∀clean
␈↓representation␈α⊂with␈α⊂comprehensible␈α∂algorithms.␈α⊂ Once␈α⊂you've␈α∂studied␈α⊂the␈α⊂algorithms,␈α∂efficiencies
␈↓might␈α⊂come␈α⊂to␈α⊂mind.␈α⊂At␈α⊂that␈α⊂time␈α⊂either␈α⊂recode␈α⊂the␈α⊂problem␈α⊂using␈α⊂some␈α⊂of␈α⊂the␈α⊃obscene␈α⊂LISP
␈↓programming␈α
tricks␈α
or␈αdrop␈α
into␈α
machine␈α
language␈αif␈α
very␈α
fine␈α
control␈αis␈α
required.␈α
 Once␈αyou␈α
have
␈↓␈↓↓a␈↓␈α∂representation␈α∂it␈α∂is␈α∂easy␈α∂to␈α∂get␈α∂better␈α∂ones.␈α∂ For␈α∂example,␈α∂once␈α∂you␈α∂have␈α∂a␈α∂compiler␈α∂which␈α∂is
␈↓correct␈αit␈αis␈α
easier␈αto␈αdescribe␈αa␈α
smarter␈αone.␈α This␈αsection␈α
will␈αdescribe␈αother␈α
representations␈αthan
␈↓LISP␈α
binary␈α
trees␈α
and␈α
will␈α
show␈α
some␈α
ways␈α
of␈α
increasing␈α
the␈α
efficiency␈α
of␈α
LISP␈α
programs



␈↓␈↓ ¬v␈↓↓8.1  Bit-tables␈↓


␈↓Bit␈α
tables:␈αIn␈α
the␈α
marking␈αphase␈α
of␈αa␈α
garbage␈α
collector␈αit␈α
is␈αnecessary␈α
to␈α
record␈αthe␈α
marking␈αof␈α
each
␈↓word.␈α On␈αmany␈αmachines␈αthe␈αmarking␈αof␈αa␈αword␈αis␈αsignified␈αby␈αsetting␈αa␈αbit␈αin␈αa␈αbit␈αtable␈αor␈αbit
␈↓map.␈α
 For␈α
example:
␈↓␈↓↓8.1␈↓ 	{Bit-tables     163␈↓











␈↓␈↓ ¬λ␈↓↓Bit map for garbage collector␈↓ 

␈↓This␈α
might␈α∞be␈α
done␈α∞for␈α
several␈α
reasons.␈α∞ The␈α
natural␈α∞choice␈α
of␈α
setting␈α∞a␈α
mark-␈α∞bit␈α
in␈α∞the␈α
actual
␈↓word␈α
being␈α
marked␈α
may␈α
not␈α
be␈α
possible␈α
or␈α
not␈α
the␈α
best␈α
strategy:

␈↓1.␈α
 for␈α
words␈α
in␈α
FS,␈α
there␈α
is␈α
no␈α
room␈α
if␈α
each␈α
word␈α
contains␈α
exactly␈α
two␈α
addresses;␈α
or

␈↓2.␈α
the␈α
word␈α
is␈α
in␈α
FWS␈α
and␈α
the␈α
meaning␈α
of␈α
the␈α
information␈α
stored␈α
there␈α
would␈α
be␈α
changed;

␈↓3.␈α⊂also␈α∂the␈α⊂garbage␈α∂collector␈α⊂must␈α∂initialize␈α⊂all␈α∂the␈α⊂mark-bits␈α∂to␈α⊂zero␈α∂before␈α⊂the␈α⊂actual␈α∂marking
␈↓␈↓ α(process␈α∞begins␈α∞(look␈α∞at␈α∞the␈α∞g.c.␈α∞algorithm).␈α∞ It␈α∞is␈α∞faster␈α∞on␈α∞most␈α∞machines␈α∞to␈α∞zero␈α∞a␈α
whole
␈↓␈↓ α(table␈α
rather␈α
than␈α
zero␈α
single␈α
bits␈α
in␈α
separate␈α
words;␈α
and␈α
finally

␈↓4.␈α
in␈α
garbage␈α
collectors␈α
for␈α
more␈α
complicated␈α∞data␈α
structures,␈α
marking␈α
with␈α
a␈α
bit␈α
table␈α∞becomes␈α
a
␈↓␈↓ α(necessity.



␈↓␈↓ ¬6␈↓↓8.2  Vectors and arrays␈↓


␈↓␈↓↓Vectors␈↓:␈α∂Vectors␈α∂(or␈α∂one␈α∂dimensional␈α∂arrays)␈α∂are␈α∂usually␈α∂stored␈α∂sequentially␈α∂in␈α∂memory.␈α∞ Simple
␈↓vectors␈α∂are␈α∞usually␈α∂stored␈α∞one␈α∂element␈α∞to␈α∂a␈α∞memory␈α∂location␈α∞though␈α∂this␈α∞is␈α∂not␈α∞a␈α∂necessity.␈α∞ For
␈↓example␈αa␈αcomplex␈αvector␈αis␈α
very␈αnaturally␈αstored␈αas␈αpairs␈αof␈α
cells;␈αor␈αif␈αperhaps␈αyou␈α
would␈αallow
␈↓vectors␈αof␈αnon-homogeneous␈αdata␈αmodes,␈αeach␈αelement␈αwould␈αhave␈αto␈αinclude␈αtype␈αinformation.␈α In
␈↓any␈α⊂case,␈α⊂most␈α⊂languages␈α⊂make␈α⊂some␈α⊂restrictions␈α∂on␈α⊂the␈α⊂behavior␈α⊂of␈α⊂vectors␈α⊂such␈α⊂that␈α∂efficient
␈↓accessing␈α
of␈αelements␈α
can␈αbe␈α
made.␈α
 There␈αis␈α
a␈αnatural␈α
simulation␈α
of␈αa␈α
stack␈αas␈α
a␈α(sequential)␈α
vector
␈↓with␈α
access␈α
to␈α
the␈α
stack␈α
made␈α
via␈α
a␈α
global␈α
pointer␈α
to␈α
the␈α
vector.

␈↓␈↓↓Arrays␈↓:␈α⊂Arrays␈α⊂are␈α⊂multi-dimensional␈α⊂data␈α⊃structures.␈α⊂ Since␈α⊂most␈α⊂machine␈α⊂memories␈α⊃are␈α⊂linear
␈↓devices␈αwe␈α
must␈αmap␈αarrays␈α
onto␈αa␈α
linear␈αrepresentation.␈α We␈α
will␈αrestrict␈αattention␈α
fo␈αthe␈α
case␈αof
␈↓two-dimensional␈αarrays.␈α Most␈αof␈αthe␈αdiscussion␈αgeneralizes␈αvery␈αnaturally.␈α A␈αvery␈αcommon␈αdevice
␈↓is␈α∂to␈α∂store␈α∞the␈α∂array␈α∂by␈α∂rows;␈α∞that␈α∂is,␈α∂each␈α∂row␈α∞is␈α∂stored␈α∂sequentially,␈α∂first,␈α∞row␈α∂1;␈α∂then␈α∂row␈α∞2,...
␈↓Given␈αthis␈αrepresentation␈αthere␈αis␈αa␈αtrivial␈αcalculation␈αto␈αfind␈αthe␈αlocation␈αof␈αan␈αarbitrary␈αelement,
␈↓A[i;j],␈αif␈αwe␈αknow␈αthe␈αlocation␈αof␈αthe␈α
first␈αelement,␈αA[1;1]␈αand␈αthe␈αextent␈αof␈αthe␈αdimensions␈α
of␈αthe
␈↓array.␈α
 For␈α
an␈α
array␈α
A[1:M;␈α
1:N]
␈↓␈↓↓164  Storage structures and efficiency␈↓ 48.2␈↓


␈↓␈↓ ∧Uloc[A[i;j]] = loc [a[1;1]] + (i-1)*N + (j-1) 

␈↓In␈α
languages␈α∞like␈α
Fortran␈α
which␈α∞require␈α
that␈α∞the␈α
size␈α
of␈α∞the␈α
array␈α
be␈α∞known␈α
at␈α∞compile-time␈α
the
␈↓compiler␈α∂can␈α∂generate␈α∂the␈α∂accessing␈α∂code␈α⊂without␈α∂problem.␈α∂ Languages,␈α∂like␈α∂Algol␈α∂60,␈α⊂and␈α∂some
␈↓versions␈αof␈αLISP␈αwhich␈αallow␈αthe␈αsize␈αof␈αan␈αarray␈αto␈αbe␈αdetermined␈αat␈αruntime␈αrequire␈αa␈αbit␈αmore
␈↓care.␈α Algol␈α60,␈αfor␈αexample␈αrequires␈αthat␈αyou␈αdeclare␈αthe␈αtype␈α(real,␈αboolean,␈αetc.)␈αof␈αthe␈αarray␈αand
␈↓specify␈αthe␈αnumber␈αof␈αdimensions␈αin␈αthe␈αarray,␈αbut␈αyou␈αcan␈αpostpone␈αuntil␈αruntime␈αthe␈αdesignation
␈↓of␈α∩the␈α∩size␈α∩of␈α∩each␈α∩dimension.␈α∩ To␈α⊃handle␈α∩this␈α∩complexity␈α∩a␈α∩dope␈α∩vector␈α∩is␈α∩introduced.␈α⊃The
␈↓compiler␈αcan␈αdetermine␈αthe␈αsize␈αof␈αthe␈αdope␈αvector,␈αbut␈αnot␈αthe␈αcontents.␈α The␈αdope␈αvector␈αis␈αfilled
␈↓in␈αat␈α
runtime␈αand␈α
contains␈αinformation␈α
about␈αthe␈αactual␈α
extent␈αof␈α
the␈αarray␈α
bounds.␈α Also␈αsince␈α
the
␈↓size␈α∂of␈α∂the␈α∂array␈α∂is␈α∂not␈α∂known,␈α∂the␈α∂compiler␈α∂cannot␈α∂allocate␈α∂space␈α∂for␈α∂the␈α∂array␈α⊂elements.␈α∂ The
␈↓allocation␈αmust␈αbe␈αdone␈αat␈αruntime.␈α When␈αthe␈αarray␈αdeclaration␈αis␈αexecuted␈αwe␈αmust␈αknow␈αall␈αthe
␈↓information␈αabout␈αthe␈αarray.␈α At␈αthat␈αtime␈αwe␈αadd␈αthe␈αarray-bound␈αinformation␈αto␈αthe␈αdope␈αvector
␈↓and␈αadd␈αinformation␈αtelling␈αwhere␈αto␈αfind␈αthe␈αarray␈αelements.␈α Assume␈αthat␈αthe␈αarray␈αelements␈αare
␈↓stored␈α∞by␈α∞rows.␈α
 Look␈α∞at␈α∞the␈α∞calculation␈α
of␈α∞the␈α∞location␈α∞of␈α
element,␈α∞A[i;j].␈α∞ For␈α∞specific␈α
execution
␈↓ofan␈αarray␈αdeclaration␈αmuch␈αof␈αthis␈αinformation␈αis␈αconstatnt;␈αthe␈αlocation␈αof␈αthe␈αarray␈αelements,␈αin
␈↓particular,␈α
A[1;1]␈α
and␈α
the␈α
number␈α
of␈α
columns,␈α
N,␈α
is␈α
fixed.␈α
 Thus␈α
we␈α
rewrite␈α
the␈α
calculation␈α
as:

␈↓␈↓ αXconstant part␈↓ ε8variable␈α
part

␈↓␈↓ αX [loc [A[1;1]]-N-1] +␈↓ ε8␈α
(i*N+j)

␈↓The␈α
constant␈α
part␈α
is␈α
stored␈α
in␈α
the␈α
dope␈α
vector.␈α
 When␈α
we␈α
wish␈α
to␈α
reference␈α
an␈α
element␈α∞A[i;j]␈α
we
␈↓need␈α
only␈α
compute␈α
the␈α
variable␈α
part␈α
and␈α
add␈α
it␈α
to␈α
the␈α
constant␈α
part.

␈↓The␈α
dope␈α
vector␈α
for␈α
A␈α
[1:M;␈α
1:N]␈α
perhaps␈α
might␈α
contain











␈↓There␈α
is␈α
another␈α
scheme␈α
for␈α
storing␈α
arrays␈αwhich␈α
is␈α
used␈α
in␈α
some␈α
of␈α
the␈α
Burroughs␈αmachines.␈α
Each
␈↓row␈α∃is␈α∃stored␈α∃sequentially␈α∃and␈α∀access␈α∃to␈α∃separate␈α∃rows␈α∃is␈α∀made␈α∃through␈α∃a␈α∃device␈α∃called␈α∀a
␈↓`mother-vector'.␈α
 The␈α
mother␈α
vector␈α
is␈α
a␈α
vector␈α
of␈α
pointers␈α
to␈α
the␈α
rows.␈α
 Thus:
␈↓␈↓↓8.2␈↓ λ⎇Vectors and arrays     165␈↓













␈↓Notice␈αthat␈αthe␈αaccessing␈αcomputation␈αis␈αvery␈αcheap.␈α Another␈αeffect␈αis␈αthat␈αall␈αrows␈αneed␈αnot␈αbe␈αin
␈↓memory␈α∞at␈α∞once.␈α∞ On␈α∞a␈α∞paging␈α∞or␈α
segmenting␈α∞machine␈α∞(we␈α∞will␈α∞talk␈α∞about␈α∞machine␈α
organization
␈↓later)␈α∞this␈α∞array␈α∞organization␈α∞can␈α∞be␈α
helpful.␈α∞ If␈α∞an␈α∞access␈α∞to␈α∞a␈α
row␈α∞not␈α∞in␈α∞core␈α∞is␈α∞made,␈α∞a␈α
`page
␈↓fault'␈α⊂is␈α⊂raised;␈α⊂the␈α⊃monitor␈α⊂brings␈α⊂the␈α⊂row␈α⊂into␈α⊃memory␈α⊂and␈α⊂the␈α⊂computation␈α⊃continues.␈α⊂ The
␈↓mother-vector␈α
scheme␈α
generalizes␈α
nicely␈α
to␈αmultidimensionality␈α
and␈α
can␈α
be␈α
used␈α
in␈αconjunction␈α
with
␈↓a␈α
dope␈α
vector.

␈↓A␈α
typical␈α
implementation␈α
on␈α
an␈α
array␈α
facility␈α
in␈α
LISP␈α
would␈α
include␈α
a␈α
declaration:

␈↓␈↓αarray␈↓␈↓α[␈↓<identifier>;<type>;<bounds>;␈α⊂...␈α∂<bounds>],␈α⊂where␈α⊂the␈α∂identifier␈α⊂names␈α∂the␈α⊂array;␈α⊂the␈α∂type
␈↓␈↓ αhcould␈α∞be␈α
numeric␈α∞or␈α
sexpr;␈α∞and␈α∞finally␈α
a␈α∞declaration␈α
of␈α∞upper␈α
and␈α∞lower␈α∞bounds␈α
for
␈↓␈↓ αheach␈αdimension␈αwould␈α
be␈αneeded.␈α ␈↓αarray␈↓␈αis␈α
a␈αspecial␈αform␈αwhose␈α
effect␈αis␈αto␈α
make␈αthe
␈↓␈↓ αharray␈α
name␈α
a␈α
␈↓αSUBR␈↓,␈α
whose␈α
code␈α
is␈α
the␈α
calculation␈α
of␈α
the␈α
dope␈α
vector.␈α
Thus:
















␈↓␈↓ αhIf␈αwe␈αare␈αto␈αstore␈αsexprs␈αin␈αthe␈αarray␈αthen␈αthe␈αgarbage␈αcollector␈αmust␈αbe␈αable␈αto␈αmark
␈↓␈↓ αhthe␈α
entries.␈α
This␈α
is␈α
the␈α
reason␈α
for␈α
including␈α
type␈α
information.

␈↓␈↓ αhWhen␈α∞an␈α∞array␈α∞element␈α∞is␈α∞to␈α∞be␈α∞referenced,␈α∞then␈α∞the␈α∞subscripts␈α∞are␈α∂evaluated␈α∞(recall
␈↓␈↓ αhthat␈α
the␈α
array␈α
name␈α∞was␈α
declared␈α
as␈α
a␈α
␈↓αSUBR␈↓)␈α∞and␈α
the␈α
dope␈α
vector␈α
code␈α∞is␈α
executed,
␈↓␈↓ αhresulting␈α
in␈α
a␈α
reference␈α
to␈α
the␈α
appropriate␈α
cell.
␈↓␈↓↓166  Storage structures and efficiency␈↓ 48.2␈↓


␈↓We␈α
also␈α
must␈α
be␈α
able␈α
to␈α
store␈α
information␈α
in␈α
the␈α
array.

␈↓␈↓αstore[␈↓<name>[<subscr>;␈α∂...␈α∞<subscr>];<value>]␈α∂:␈α∞␈↓αstore␈↓␈α∂is␈α∂a␈α∞special␈α∂form␈α∞whose␈α∂effect␈α∞is␈α∂to␈α∂store␈α∞the
␈↓␈↓ αhvalue␈α
of␈α
<value>␈α
in␈α
the␈α
designated␈α
array␈α
element.



␈↓␈↓ ¬∃␈↓↓8.3  strings and linear LISP␈↓


␈↓Strings␈α∞and␈α
string␈α∞processors␈α
are␈α∞an␈α
important␈α∞class␈α
of␈α∞data␈α
structures␈α∞and␈α∞algorithms.␈α
 Powerful
␈↓string␈α
processing␈α
is␈α∞a␈α
necessity␈α
for␈α
any␈α∞well␈α
developed␈α
compiler-writing␈α
system.␈α∞ The␈α
organization
␈↓and␈αimplementation␈αof␈αa␈αgeneral␈αstring␈αprocessor␈αdirectly␈αparallels␈αLISP.␈α In␈αfact␈αa␈αsubset␈αof␈αLISP,
␈↓linear␈α
LISP,␈α
is␈α
a␈α
nice␈α
notation␈α
for␈α
string␈α
algorithms.

␈↓A␈α
string␈αis␈α
a␈αsequence␈α
of␈α
characters.␈α A␈α
reasonable␈αlanguage␈α
(not␈α
PL/1)␈αfor␈α
string␈αprocessing␈α
should
␈↓allow␈α
the␈αcreation␈α
of␈α
strings␈αof␈α
arbitrary␈α
length␈αat␈α
runtime;␈αit␈α
should␈α
allow␈αthe␈α
generation␈α
of␈αnew
␈↓strings␈α
and␈α
the␈αdecomposition␈α
of␈α
existing␈α
strings.␈α If␈α
strings␈α
of␈α
arbitrary␈αlength␈α
are␈α
to␈α
be␈αcreated,␈α
an
␈↓organization␈αsimilar␈αto␈αFS␈αin␈αLISP␈αcan␈αbe␈αused␈αwith,␈αperhaps,␈αa␈αstring␈αgarbage␈αcollector.␈α We␈αwill
␈↓assume␈α⊃this␈α∩most␈α⊃general␈α∩case.␈α⊃ The␈α⊃machine␈α∩memory␈α⊃will␈α∩contain␈α⊃at␈α⊃least␈α∩a␈α⊃string␈α∩space,␈α⊃an
␈↓evaluator,␈α
a␈α
symbol␈α
table,␈α
and␈α
a␈α
garbage␈α
collector.

␈↓String␈αspace␈αis␈αa␈αlinear␈αsequence␈αof␈αcells,␈αeach␈αof␈αwhich␈αcan␈αcontain␈αexactly␈αone␈αcharcter.␈α A␈αstring
␈↓will␈α
be␈αrepresented␈α
as␈αa␈α
sequence␈αof␈α
sequential␈αcharacter␈α
cells.␈α A␈α
string␈αvariable␈α
will␈αbe␈α
an␈αentry␈α
in
␈↓the␈α
symbol␈α∞table;␈α
the␈α
current␈α∞value␈α
of␈α∞the␈α
variable␈α
will␈α∞be␈α
represented␈α
as␈α∞a␈α
pair;␈α∞character␈α
count
␈↓and␈α
a␈α
pointer␈α
to␈α
the␈α
beginning␈α
of␈α
the␈α
character␈α
sequence.

␈↓Thus:





␈↓We␈α
must␈α
make␈α
some␈α∞decisions␈α
about␈α
how␈α
we␈α∞manipulate␈α
strings:␈α
when␈α
we␈α∞perform␈α
␈↓αx␈α
←␈α
y␈↓,␈α∞do␈α
we
␈↓copy␈α
the␈α
symbol␈α∞table␈α
pair␈α
of␈α∞␈↓αy␈↓␈α
into␈α
that␈α
of␈α∞␈↓αx␈↓,␈α
or␈α
do␈α∞we␈α
make␈α
a␈α
new␈α∞string␈α
isomorphic␈α
to␈α∞␈↓αy␈↓␈α
and
␈↓point␈α␈↓αx␈↓␈αat␈αit.␈α It␈αmakes␈αa␈αdifference.␈α We␈αwill␈αchoose␈αthe␈αformer,␈αcopying␈αonly␈αthe␈α`descriptor',␈αthat
␈↓is,␈α∪we␈α∩will␈α∪share␈α∪strings␈α∩(and␈α∪substrings)␈α∩wherever␈α∪possible.␈α∪This␈α∩decision␈α∪makes␈α∪the␈α∩storage
␈↓requirements␈α∞less␈α∞expensive,␈α
but␈α∞will␈α∞make␈α∞our␈α
life␈α∞more␈α∞difficult␈α
when␈α∞we␈α∞worry␈α∞about␈α
garbage
␈↓collection.␈α
 There␈α
are␈α
three␈α
primitive␈α
functions:␈α
␈↓αfirst␈↓,␈α
␈↓αrest␈↓,␈α
␈↓αconcat␈↓␈α
(read:␈α
␈↓αcar␈↓,␈α
␈↓αcdr␈↓,␈α
␈↓αcons␈↓).

␈↓␈↓αfirst[x]␈↓␈αis␈αthe␈αfirst␈αcharacter␈αof␈αthe␈αstring␈αrepresented␈αby␈α␈↓αx␈↓.␈α ␈↓αfirst␈↓␈αis␈αundefined␈αfor␈αthe␈αempty␈αstring.
␈↓␈↓ αhFor␈α
example:

␈↓␈↓ ∧←␈↓αfirst[ABC]␈↓ is ␈↓αA; first[␈↓∧␈↓α]␈↓ is undefined. 
␈↓␈↓↓8.3␈↓ λ<strings and linear LISP     167␈↓


␈↓␈↓αrest[x]␈↓␈α
is␈α∞the␈α
string␈α
of␈α∞characters␈α
which␈α∞remains␈α
when␈α
the␈α∞first␈α
character␈α
of␈α∞the␈α
string␈α∞is␈α
deleted.
␈↓␈↓αrest␈↓␈α
is␈α
also␈α
undefined␈α
for␈α
the␈α
empty␈α
string.␈α
For␈α
example:

␈↓␈↓ ¬j␈↓αrest[ABC]␈↓ is ␈↓αBC␈↓ 

␈↓␈↓αconcat[x;y]␈↓␈α∂is␈α∞a␈α∂function␈α∂of␈α∞two␈α∂arguments.␈α∞ ␈↓αx␈↓␈α∂is␈α∂a␈α∞character;␈α∂␈↓αy␈↓␈α∞is␈α∂a␈α∂string.␈α∞␈↓αconcat␈↓␈α∂forms␈α∂a␈α∞string
␈↓␈↓ αhconsisting␈α
of␈α
the␈α
concatenation␈α
a␈α
copy␈α
of␈α
␈↓αx␈↓␈α
and␈α
a␈α
copy␈α
of␈α
the␈α
string,␈α
␈↓αy␈↓.␈α
 For␈α
example:

␈↓␈↓ βp␈↓αconcat[A;BC]␈↓ is ␈↓αABC␈↓ There are three string predicates: 
␈↓␈↓ ¬¬␈↓αchar␈↓␈↓α[x]␈↓:  is ␈↓αx␈↓ a single character?
␈↓␈↓ ¬
␈↓αnull␈↓␈↓α[x]␈↓:  is ␈↓αx␈↓ the empty string?
␈↓␈↓ ∧U␈↓αx = y␈↓:  are ␈↓αx␈↓ and ␈↓αy␈↓ the same character?

␈↓For example:

␈↓␈↓ ¬t␈↓αchar[A] ␈↓is true
␈↓␈↓ ¬f␈↓αchar[AB] ␈↓is false
␈↓␈↓ ¬?␈↓αAB = AB ␈↓is undefined

␈↓Now␈αto␈αimplementations:␈α␈↓αfirst␈↓␈αgenerates␈αa␈αcharacter␈αcount␈αof␈α1␈αand␈αa␈αpointer␈αto␈αthe␈αfirst␈αcharacter
␈↓of␈α∞the␈α∞parent␈α∞string.␈α∞ ␈↓αrest␈↓␈α∞generates␈α∞a␈α∞character␈α∂count␈α∞of␈α∞one␈α∞less␈α∞than␈α∞that␈α∞of␈α∞the␈α∞parent␈α∂and␈α∞a
␈↓pointer␈α
to␈α
the␈α
second␈α
character␈α
of␈α
the␈α
parent␈α
string.

␈↓␈↓αconcat␈↓␈αis␈αa␈αbit␈αmore␈αproblematic.␈α We␈αcopy␈α␈↓αx␈↓␈αand␈αcopy␈α␈↓αy␈↓,␈αgenerate␈αa␈αcharacter␈αcount␈αof␈αthe␈αsum␈αof
␈↓those␈αof␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓,␈αand␈αgenerate␈αa␈αpointer␈αto␈α
the␈αcharacter␈αof␈αthe␈αcopy␈αof␈α␈↓αx␈↓.␈α The␈αcopies␈αare␈αmade␈α
in
␈↓the␈αfree␈αstring␈αspace␈αpointed␈αto␈αby␈αthe␈α␈↓↓string␈αspace␈αpointer␈↓.␈α The␈αobvious␈αquestion␈αof␈αwhat␈αto␈αdo
␈↓when␈αstring␈αspace␈αis␈αexhausted␈αwill␈αbe␈α
postponed␈αfor␈αa␈αmoment.␈α The␈αimplementations␈αof␈αthe␈α
three
␈↓predicates␈α
are␈α
easy:␈αwe␈α
will␈α
blur␈αthe␈α
distinction␈α
between␈αcharacters␈α
and␈α
strings␈αof␈α
length␈α
1.␈α Thus
␈↓␈↓αchar␈↓␈α∞need␈α∞check␈α
the␈α∞character␈α∞count.␈α
 ␈↓αnull␈↓␈α∞says␈α∞character␈α
count␈α∞is␈α∞0.␈α
 What␈α∞about␈α∞=␈α∞?␈α
Obviously
␈↓characters␈α
are␈α
not␈α
stored␈α
uniquely,␈α
so␈α
we␈α
must␈α
make␈α
an␈α
actual␈α
character␈α
comparison.

␈↓Now␈αgarbage␈αcollection.␈α In␈αsome␈αways␈αa␈αstring␈αgarbage␈αcollector␈αis␈αsimpler␈αand␈αin␈αsome␈αways␈αmore
␈↓difficult␈αthan␈αa␈αcollector␈αof␈αlist-structure.␈α The␈α
marking␈αphase␈αis␈αmuch␈αsimpler:␈αusing␈αthe␈α
descriptor
␈↓in␈αthe␈αsymbol␈αtable,␈αmark␈αthe␈αcharacter␈αstring.␈α
 Since␈αwe␈αare␈αsharing␈αsubstrings,␈αwe␈αcannot␈αstop␈α
the
␈↓marking␈α⊃simply␈α⊃because␈α⊃we␈α⊂have␈α⊃encountered␈α⊃a␈α⊃previously␈α⊂marked␈α⊃character.␈α⊃But␈α⊃at␈α⊃least␈α⊂the
␈↓marking␈α
is␈αnot␈α
recursive.␈α However,␈α
the␈α
collection␈αphase␈α
needs␈αto␈α
be␈αmore␈α
sophisticated␈α
for␈αstring
␈↓processors.␈α∞ Since␈α∞strings␈α∞are␈α∂stored␈α∞linearly␈α∞(or␈α∞sequentially),␈α∞a␈α∂fragmented␈α∞string␈α∞space␈α∞list␈α∂is␈α∞of
␈↓little␈α
use.␈α
 Thus␈α
we␈α
must␈α
compact␈α
all␈α
the␈α
referenceable␈α
strings␈α
into␈α
one␈α
end␈α
of␈α
string␈α
space,␈αfreeing␈α
a
␈↓linear␈αblock␈αfor␈αthe␈αnew␈αfree␈αstring␈αspace.␈α Since␈αwe␈αare␈αsharing␈αsubstrings␈αa␈αlittle␈αcare␈αneeds␈αto␈αbe
␈↓exercised.␈α
 When␈α
we␈α
move␈α
a␈α
string,␈α
obviously␈α
the␈α
descriptor␈α
of␈α
any␈α
variable␈α
referencing␈α
any␈α
part␈α
of
␈↓that␈α
parent␈α
string␈α
must␈α
be␈αchanged␈α
to␈α
reflect␈α
the␈α
new␈α
location.␈α So␈α
before␈α
we␈α
begin␈α
the␈αrelocation␈α
of
␈↓strings␈α∂we␈α⊂sort␈α∂the␈α⊂string␈α∂descriptors␈α⊂on␈α∂the␈α∂basis␈α⊂of␈α∂their␈α⊂pointers␈α∂into␈α⊂string␈α∂space.␈α⊂ We␈α∂then
␈↓recognize␈α
each␈α
parent␈α
string,␈α
moving␈α
it␈α
down␈α
into␈α
freed␈α
locations␈α
and␈α
updating␈α
the␈αaddress␈α
pointers
␈↓of␈α∂any␈α∂substrings.␈α∂ We␈α⊂continue␈α∂this␈α∂process.␈α∂ Eventually␈α∂all␈α⊂strings␈α∂will␈α∂be␈α∂compacted␈α⊂down␈α∂in
␈↓string␈α∞space.␈α∞ The␈α∞free␈α∞space␈α∞pointer␈α∞will␈α∞be␈α∞set␈α∞and␈α∞the␈α∞computation␈α∞can␈α∞continue.␈α
 Compacting
␈↓garbage␈α
collectors␈α
can␈α
be␈α
adapted␈α
for␈α
use␈α
in␈α
LISP␈α
or␈α
more␈α
general␈α
types␈α
of␈α
data␈α
structures.
␈↓␈↓↓168  Storage structures and efficiency␈↓ 18.4␈↓


␈↓␈↓ ¬?␈↓↓8.4  ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓␈↓α


␈↓We␈αwill␈αfirst␈αlook␈αat␈αsome␈αLISP␈αcoding␈αtricks␈αwhich␈αwhen␈αused␈αjudiciously,␈αcan␈αincrease␈αefficiency,
␈↓but␈α⊃when␈α⊂used␈α⊃in␈α⊂a␈α⊃cavalier␈α⊃manner␈α⊂can␈α⊃result␈α⊂in␈α⊃mystery.␈α⊃ First,␈α⊂LISP␈α⊃does␈α⊂an␈α⊃awful␈α⊃lot␈α⊂of
␈↓copying.␈α
 Consider:

␈↓␈↓α␈↓ β.append <= λ[[x;y][null[x] → y;T → cons[car[x];append[cdr[x];y]]]]␈↓ 

␈↓This␈α
function␈α
copies␈α
␈↓αx␈↓␈α∞onto␈α
the␈α
front␈α
of␈α
␈↓αy␈↓.␈α∞Note:␈α
␈↓αy␈↓␈α
is␈α
not␈α
copied.␈α∞ Or␈α
recall␈α
the␈α
␈↓αsubst␈↓␈α∞function:␈α
it
␈↓generates␈αa␈αcopy␈αwith␈αthe␈αcorrect␈αsubstitutions␈αmade.␈α The␈αcopying␈αis␈αnecessary␈αin␈αgeneral.␈αIt␈αkeeps
␈↓unsavory␈α
side␈α
effects␈α
from␈α
happening.

␈↓Let's␈α∂look␈α∂at␈α∂␈↓αappend[(A␈α∂B␈α∞C);(D␈α∂E␈α∂F)]␈↓.␈α∂ It␈α∂appears␈α∂that␈α∞we␈α∂could␈α∂get␈α∂the␈α∂appropriate␈α∂effect␈α∞of
␈↓␈↓αappend␈↓␈α∂by␈α∞␈↓αcdr␈↓-ing␈α∂down␈α∂the␈α∞list␈α∂␈↓α(A␈α∞B␈α∂C)␈↓␈α∂until␈α∞we␈α∂found␈α∞the␈α∂terminator,␈α∂then␈α∞replace␈α∂it␈α∂with␈α∞a
␈↓pointer␈α
to␈α
the␈α
list␈α
␈↓α(D␈α
E␈α
F)␈↓.␈α
 Thus:







␈↓What's␈α
wrong␈α
here?␈α
 Consider␈α
the␈α
sequence␈α
of␈α
statements:

␈↓α␈↓ ¬_i ← (A,B,C)

␈↓α␈↓ ¬_j ← (D,E,F)

␈↓α␈↓ ¬_k ← append[i;j]

␈↓Then␈α⊃if␈α⊃␈↓αappend␈↓␈α∩worked␈α⊃as␈α⊃advertised␈α∩above,␈α⊃(changing␈α⊃the␈α∩␈↓αcdr␈↓␈α⊃of␈α⊃the␈α∩last␈α⊃element␈α⊃of␈α∩␈↓αi␈↓)␈α⊃the
␈↓following␈α
evil␈α
would␈α
be␈α∞perpetrated:␈α
the␈α
value␈α
of␈α
␈↓αi␈↓␈α∞would␈α
be␈α
changed␈α
surreptitiously!!␈α
 ␈↓αi␈↓␈α∞now␈α
has
␈↓the␈αvalue␈α␈↓α(A,B,C,D,E,F)␈↓.␈α Language␈αfeatures␈αwhich␈αdo␈αthis␈αare␈αevil.␈α It␈αis,␈αhowever,␈αquite␈αuseful␈αto
␈↓be␈αevil␈αsometimes.␈α Notice␈αthat␈αany␈αvalue␈αwhich␈αwas␈αsharing␈αpart␈αof␈αthe␈αstructure␈αof␈α␈↓αi␈↓␈αwill␈αalso␈αbe
␈↓changed.␈α⊂ This␈α∂can␈α⊂cause␈α⊂real␈α∂mystery!!␈α⊂Well␈α⊂the␈α∂world␈α⊂is␈α∂good,␈α⊂and␈α⊂␈↓αappend␈↓␈α∂does␈α⊂not␈α⊂work␈α∂as
␈↓above.␈α The␈α
LISP␈αfunction␈α
which␈α␈↓↓does␈↓␈α
work␈αthis␈αway␈α
is␈αcalled␈α
␈↓αnconc␈↓.␈α It␈α
can␈αbe␈α
defined␈αin␈αterms␈α
of
␈↓a␈α
primitive␈α
obscene␈α
function,␈α
␈↓αrplacd␈↓.␈α
 There␈α
are␈α
two␈α
primitive␈α
obscene␈α
functions:

␈↓␈↓αrplaca␈↓␈↓α[x;y]␈↓␈α
replace␈α
the␈α
␈↓αcar␈↓-part␈α
of␈α
␈↓αx␈↓␈α
with␈α
␈↓αy␈↓.
␈↓␈↓↓8.4␈↓ 	∪␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓     169␈↓α


␈↓␈↓αrplacd␈↓␈↓α[x;y]␈↓␈α
replace␈α
the␈α
␈↓αcdr␈↓-part␈α
of␈α
␈↓αx␈↓␈α
with␈α
␈↓αy␈↓.







␈↓Thus␈α
␈↓αnconc␈↓␈α
can␈α
be␈α
defined␈α
as:

␈↓αnconc <= λ[[x;y]prog␈↓ βx[[z]
␈↓α␈↓ βx [null[x] → return[y]];
␈↓α␈↓ βx z ← x;
␈↓α␈↓ βxa[null[cdr[z]] → rplacd[z;y];return [x]];
␈↓α␈↓ βx z ←cdr [z];
␈↓α␈↓ βx go[a] ]]


␈↓These␈α⊂functions␈α⊃must␈α⊂be␈α⊃used␈α⊂with␈α⊃extreme␈α⊂caution.␈α⊃ They␈α⊂are␈α⊃not␈α⊂recommended␈α⊃for␈α⊂amateur
␈↓hacking.␈α They␈αare␈αintroduced␈αhere␈αsimply␈αto␈αshow␈αthat␈αit␈αis␈αpossible␈αto␈αimprove␈αon␈αthe␈αefficiency
␈↓of␈α
pure␈α
algorithms␈α
by␈α
resorting␈α
to␈α
these␈α
coding␈α
tricks.␈α
 Consider:

␈↓α␈↓ ¬_x ← (NOTHING CAN GO WRONG);
␈↓α␈↓ ¬_rplacd[cdddr[x];cddr[x]];
␈↓α␈↓ ¬_print[x];


␈↓So␈α∂we␈α⊂can␈α∂use␈α∂␈↓αrplacd␈↓␈α⊂to␈α∂generate␈α⊂circular␈α∂lists␈α∂(and␈α⊂to␈α∂generate␈α∂wall␈α⊂paper␈α∂by␈α⊂printing␈α∂them!!).
␈↓Circular␈α⊃lists␈α⊃cannot␈α⊃be␈α⊃generated␈α⊃in␈α⊃LISP␈α⊃without␈α⊃functions␈α⊃like␈α⊃␈↓αrplaca␈↓␈α⊃and␈α⊃␈↓αrplacd␈↓.␈α⊃ See␈α⊂the
␈↓problem␈α
on␈α
page␈α
96.␈α
 In␈α
general,␈α
to␈α
circularize␈α
a␈α
non-empty␈α
list,␈α
␈↓αx␈↓,␈α
␈↓αrplacd[last[x];x]␈↓␈α
suffices␈α
where:

␈↓␈↓ ∧$␈↓αlast <=λ[[x][null[cdr[x]] → x; T → last[cdr[x]]]]␈↓ 

␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓␈↓↓1.␈↓␈α
What␈α
is␈α
the␈α
effect␈α
of␈α
evaluating␈α
␈↓αrplacd[x;cdr[x]]␈↓?



␈↓␈↓ ∧R␈↓↓8.5  Applications of ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓␈↓α


␈↓We␈α⊂begin␈α⊂with␈α∂rather␈α⊂simple␈α⊂examples.␈α⊂Consider␈α∂the␈α⊂problem␈α⊂of␈α∂inserting␈α⊂an␈α⊂element␈α⊂into␈α∂the
␈↓middle␈α∞of␈α
a␈α∞list.␈α
 For␈α∞example␈α
let␈α∞␈↓αx␈↓␈α
be␈α∞the␈α
list,␈α∞␈↓α(A␈α
B␈α∞C)␈↓.␈α
 If␈α∞we␈α
wished␈α∞to␈α
insert␈α∞an␈α
atom,␈α∞say␈α
␈↓αD␈↓,
␈↓between␈α
␈↓αB␈↓␈α
and␈α
␈↓αC␈↓,␈α
we␈α
could␈α
perform:
␈↓α␈↓ ∧/x ← cons[car[x];cons[cadr[x];cons[D;cddr[x]]]].
␈↓␈↓↓170  Storage structures and efficiency␈↓ 38.5␈↓


␈↓Indeed,␈α
in␈αgeneral,␈α
we␈α
have␈αlittle␈α
choice␈αbut␈α
to␈α
recopy␈αthe␈α
the␈αinitial␈α
segment␈α
of␈α␈↓αx␈↓,␈α
adding␈α
␈↓αD␈↓␈αinto
␈↓the␈α
appropriate␈αplace.␈α
 A␈αsimilar␈α
technique␈αis␈α
obviously␈α
available␈αto␈α
delete␈αa␈α
specified␈αelement␈α
from
␈↓the␈α
interior␈α
of␈α
a␈α
list.

␈↓Careful␈α
use␈α
of␈α
␈↓αrplacd␈↓␈α
can,␈α
in␈α
some␈α
instances,␈αreplace␈α
the␈α
heavy␈α
use␈α
of␈α
␈↓αcons␈↓.␈α
 ␈↓αcons␈↓␈α
always␈αcarries␈α
with
␈↓it␈α
the␈α
threat␈α
of␈α
a␈α
call␈α
on␈α
the␈α
garbage␈α
collector.

␈↓For␈α
example,␈α
given␈α
the␈α
list␈α
␈↓α(A␈α
B␈α
C)␈↓␈α
with␈α
pointers,␈α
␈↓αx␈↓␈α
and␈α
␈↓αy␈↓,␈α
into␈α
it␈α
as␈α
follows,






␈↓we␈α
could␈α
insert␈α
the␈α
element,␈α
␈↓αD␈↓,␈α
␈↓↓after␈↓␈α
the␈α
first␈α
element␈α
in␈α
␈↓αy␈↓␈α
by:

␈↓␈↓ ¬␈↓αrplacd[y;cons[D;cdr[y]]␈↓, giving: 






␈↓(Notice␈α
that␈α
␈↓↓one␈↓␈α
␈↓αcons␈↓␈α
is␈α
unavoidable.)

␈↓But␈αas␈α
always,␈αbe␈α
warned␈αthat␈α
the␈αvalue␈α
of␈α␈↓αx␈↓␈α
has␈αalso␈α
been␈αchanged;␈α
and␈αany␈α
sexpr␈αsharing␈αthe␈α
list
␈↓␈↓αx␈↓␈α
or␈α
␈↓αy␈↓␈α
as␈α
a␈α
sublist␈α
has␈α
also␈α
been␈α
affected.

␈↓We␈α
can␈α
also␈α
use␈α
␈↓αrplacd␈↓␈α
to␈α
delete␈α
not␈α
the␈α
␈↓↓first␈↓,␈α
but␈α
the␈α
next␈α
element,␈α
in␈α
␈↓αy␈↓␈α
by:

␈↓␈↓ ¬c␈↓αrplacd[y;cddr[y]].␈↓

␈↓Similarly,␈αwe␈αcan␈αuse␈α␈↓αrplaca␈↓␈αto␈αmodify␈αan␈αelement␈αin␈αa␈αlist␈α(or␈αsexpr).␈α To␈αchange␈αthe␈αfirst␈αelement
␈↓in␈α
the␈α
list,␈α
␈↓αy␈↓,␈α
to␈α
the␈α
sexpr,␈α
␈↓αz␈↓␈α
use

␈↓␈↓ ε	␈↓αrplaca[y;z]␈↓.

␈↓Notice␈α
that␈αthe␈α
uses␈αof␈α
␈↓αrplacd␈↓␈α
for␈αinsertion␈α
and␈αdeletion␈α
are␈α
couched␈αin␈α
terms␈αof␈α
insert␈α
␈↓↓after␈↓␈αand
␈↓delete␈α
␈↓↓after␈↓,␈α
rather␈α
than␈α
insert␈α
␈↓↓at␈↓␈α
or␈α
delete␈α
␈↓↓at␈↓.␈α
 If␈α
you␈α
look␈α
at␈α
a␈α
diagram␈α
you␈α
will␈α
see␈α
why:
␈↓␈↓↓8.5␈↓ π6Applications of ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓     171␈↓α


␈↓To␈αdelete␈αthe␈αelement␈α␈↓αB␈↓␈αrequires␈αmodifying␈αthe␈α␈↓αcdr␈↓-part␈αof␈αthe␈αpredecessor␈αcell.␈α Similarly␈αto␈αinsert
␈↓at␈α∞a␈α∞specified␈α∞cell.␈α∂How␈α∞could␈α∞we␈α∞write␈α∞such␈α∂modifying␈α∞functions?␈α∞ A␈α∞simple,␈α∂perhaps␈α∞inefficient
␈↓scheme,␈αwould␈αbe␈αto␈αstart␈αanother␈αpointer␈αfrom␈αthe␈αbeginning␈αof␈αthe␈αlist,␈αlooking␈αfor␈αthe␈αcell␈αwhose
␈↓␈↓αcdr␈↓␈α
pointed␈α
to␈α
the␈α
desired␈α
spot;␈α
then␈α
make␈α
the␈α
modification.

␈↓If␈α∞these␈α∞`modification-␈↓↓at␈↓'␈α∞functions␈α∞were␈α∞to␈α∞be␈α∞performed␈α∞very␈α∞frequently␈α∞then␈α∞it␈α∞might␈α∞be␈α
worth
␈↓starting␈α␈↓↓two␈↓␈αpointers␈αdown␈αthe␈αlist,␈αone␈αat␈α␈↓αx␈↓,␈αone␈αsay␈α␈↓αy␈↓␈αat␈α␈↓αcdr[x]␈↓,␈αas␈αabove.␈α Then␈α␈↓↓testing␈↓␈αcould␈αbe
␈↓done␈α
using␈α
␈↓αy␈↓␈α
and␈αthe␈α
␈↓↓modification␈↓␈α
could␈α
be␈α
done␈αusing␈α
␈↓αx␈↓.␈α
When␈α
we␈α
move␈α␈↓αy␈↓␈α
to␈α
␈↓αcdr[y]␈↓␈α
we␈α
move␈α␈↓αx␈↓␈α
to
␈↓␈↓αcdr[x]␈↓.␈α
 What␈α
happens␈α
if␈α
we␈α
wanted␈α
to␈αmodify␈α
␈↓↓before␈↓␈α
rather␈α
that␈α
␈↓↓at␈↓?␈α
We␈α
could␈α
proliferate␈αthe␈α
`back
␈↓pointers',␈αbut␈αperhaps␈αif␈αthis␈αkind␈αof␈αgenerality␈αis␈αrequired␈αa␈αchange␈αof␈αrepresentation␈αis␈αcalled␈αfor.
␈↓More␈α
complicated␈α
data␈α
representations␈α
are␈α
discussed␈α
in␈α
detail␈α
in␈α
Knuth's␈α
Kompendium,Khapter␈α
2.

␈↓The␈α
LISP␈α
system␈αuses␈α
␈↓αrplaca␈↓␈α
and␈α␈↓αrplacd␈↓␈α
heavily;␈α
for␈αexample,␈α
functions␈α
which␈α
modify␈αproperties
␈↓on␈α
the␈α
p-lists␈α
use␈α
these␈α
functions.␈α
Here␈α
are␈α
two␈α
p-list␈α
manipulating␈α
functions,␈α
␈↓αputprop␈↓␈α
and␈α
␈↓αremprop␈↓.

␈↓␈↓αputprop␈↓␈αis␈αa␈αfunction␈αof␈αthree␈αarguments,␈α␈↓αn␈↓,␈αan␈αatom;␈α␈↓αi␈↓,␈αan␈αindicator;␈αand␈α␈↓αv␈↓␈αa␈αvalue.␈αThe␈αeffect␈αof
␈↓␈↓ αh␈↓αputprop␈↓␈α
is␈α
to␈α
attach␈α
the␈α
indicator-value␈α
pair␈α
to␈α
␈↓αn␈↓.␈α
 If␈α
the␈α
indicator␈α
is␈α
already␈α
present
␈↓␈↓ αhthen␈α
we␈α
will␈α
simply␈α
change␈αits␈α
value␈α
to␈α
␈↓αv␈↓;␈α
if␈α
the␈αindicator␈α
is␈α
not␈α
present␈α
then␈α
we␈αwill
␈↓␈↓ αhadd␈α⊃the␈α⊃indicator-value␈α⊃pair␈α⊃to␈α⊃the␈α⊂front␈α⊃of␈α⊃the␈α⊃p-list.␈α⊃ Since␈α⊃␈↓αVALUE␈↓-cells␈α⊃have␈α⊂a
␈↓␈↓ αhslightly␈α
different␈α
format␈α
(see␈α
page␈α
88),␈α
there␈α
is␈α
special␈α
glitch␈α
for␈α
adding␈α
or␈αmodifying
␈↓␈↓ αhthem.

␈↓αputprop <= λ[[n;i;v]
␈↓α   prog[[m]
␈↓α␈↓ αλ␈↓ α8m ← cdr[n];
␈↓α␈↓ αλa␈↓ α8[eq[car[m];i] → go[b]]
␈↓α␈↓ αλ␈↓ α8m ← cddr[m];
␈↓α␈↓ αλ␈↓ α8[m → go[a]];
␈↓α␈↓ αλ␈↓ α8[eq[i;VALUE] → rplacd[n;cons[VALUE;cons[cons[NIL;v];cdr[n]]]];return[v];
␈↓α␈↓ αλ␈↓ α8 T → rplacd[n;cons[i;cons[v;cdr[n]]]];return[v]  ]
␈↓α␈↓ αλb␈↓ α8[eq[i;VALUE] → rplacd[cadr[m];v];return[v];
␈↓α␈↓ αλ␈↓ α8 T → rplaca[cdr[m];v];return[v]] ]]


␈↓Notes:

␈↓1.␈α
␈↓α[m␈α
→␈α
go[a]]␈↓␈α
is␈α
a␈α
trick␈α
you␈α
have␈α
seen␈α
before.

␈↓2.␈α
Extended␈α
conditional␈α
expressions␈α
are␈α
used␈α
here.

␈↓␈↓αremprop␈↓␈α
is␈α
simpler.

␈↓␈↓αremprop␈↓␈αis␈αa␈αpredicate,␈αwhose␈αimportance␈αlies␈αin␈αits␈αside␈αeffects.␈α␈↓αremprop␈↓␈αhas␈αtwo␈αarguments,␈α␈↓αn␈↓,␈αan
␈↓␈↓ αhatom,␈α⊂and␈α∂␈↓αi␈↓,␈α⊂an␈α∂indicator.␈α⊂If␈α∂the␈α⊂indicator␈α⊂is␈α∂found␈α⊂on␈α∂the␈α⊂p-list␈α∂of␈α⊂the␈α⊂atom,␈α∂then
␈↓␈↓ αh␈↓αremprop␈↓␈α∪removes␈α∪the␈α∪indicator-value␈α∪pair␈α∩and␈α∪returns␈α∪␈↓αT␈↓,␈α∪otherwise␈α∪␈↓αremprop␈↓␈α∩does
␈↓␈↓ αhnothing␈α
and␈α
returns␈α
␈↓αNIL␈↓.
␈↓␈↓↓172  Storage structures and efficiency␈↓ 38.5␈↓



␈↓αremprop <= λ[[n;i]
␈↓α   prog[[m]
␈↓α␈↓ αλ␈↓ α8m ← n;
␈↓α␈↓ αλa␈↓ α8[eq[cadr[m;i] → rplacd[m;cddr[m]];return[T]]
␈↓α␈↓ αλ␈↓ α8m ← cddr[m];
␈↓α␈↓ αλ␈↓ α8[m → go[a]]
␈↓α␈↓ αλ␈↓ α8return[NIL] ]]


␈↓Applications␈α
of␈α∞␈↓αrplacd␈↓␈α
will␈α∞occur␈α
inside␈α∞␈↓αratom␈↓␈α
and␈α
inside␈α∞the␈α
garbage␈α∞collector.␈α
 For␈α∞example␈α
the
␈↓sweep␈α
phase␈α
might␈α
be␈α
described␈α
as:

␈↓αsweep <= λ[[x;y]prog␈↓ ∧_[[z]
␈↓α␈↓ ∧_␈↓ ∧Hz ← NIL;
␈↓α␈↓ ∧_a␈↓ ∧H[nap[x] → z ← rplacd[x;z]]
␈↓α␈↓ ∧_␈↓ ∧Hx ← down[x];
␈↓α␈↓ ∧_␈↓ ∧H[eq[x;y] → return[z]]
␈↓α␈↓ ∧_␈↓ ∧Hgo[a] ]]


␈↓Where␈α∂␈↓αsweep␈↓␈α∂is␈α∂to␈α∂sweep␈α∂from␈α∞␈↓αx␈↓␈α∂to␈α∂␈↓αy␈↓.␈α∂ ␈↓αnap␈↓␈α∂is␈α∂a␈α∂predicate␈α∞returning␈α∂␈↓αT␈↓␈α∂just␈α∂in␈α∂the␈α∂case␈α∂that␈α∞its
␈↓argument␈αis␈αstill␈αmarked␈α`not␈αavailable';␈αand␈α␈↓αdown␈↓␈αfinds␈αthe␈α`successor'␈αof␈αits␈αargument␈αin␈αthe␈αlinear
␈↓ordering␈α
of␈α
the␈α
memory␈α
cells.

␈↓As␈α∞a␈α
final␈α∞example␈α
we␈α∞will␈α
describe␈α∞a␈α∞fixup␈α
mechanism␈α∞(see␈α
Section␈α∞7.5)␈α
using␈α∞␈↓αrplacd␈↓.␈α∞Since␈α
our
␈↓fixups␈α∞will␈α∞be␈α
very␈α∞simple␈α∞when␈α∞assembling␈α
compiled␈α∞code,␈α∞we␈α
will␈α∞chain␈α∞the␈α∞forward␈α
references
␈↓together␈α∞via␈α∞their␈α∞␈↓αcdr␈↓-parts.␈α∞If␈α∞an␈α∞atom,␈α∞␈↓αn␈↓,␈α∞is␈α∂defined␈α∞to␈α∞be␈α∞a␈α∞label␈α∞it␈α∞will␈α∞have␈α∞on␈α∞its␈α∂p-list,␈α∞the
␈↓indicator␈α
␈↓αSYM␈↓␈αand␈α
a␈αvalue␈α
representing␈αthe␈α
memory␈α
location␈αassigned␈α
to␈α␈↓αn␈↓.␈α
 In␈αthe␈α
case␈αof␈α
forward
␈↓references␈αto␈αa␈αlabel␈α␈↓αn␈↓␈αtwo␈αactions␈αmay␈αoccur.␈α
 Let␈αloc␈αbe␈αthe␈αmemory␈αlocation␈αto␈αbe␈αassigned␈αto␈α
the
␈↓instruction␈αcontaining␈α
the␈αforward␈αreference.␈α
 If␈αthis␈αis␈α
the␈αfirst␈αoccurrence␈α
of␈αa␈α
forward␈αreference
␈↓to␈α␈↓αn␈↓,␈α
then␈αthe␈αpair␈α
␈↓α(UNDEF␈α.(␈↓␈α
loc␈↓α))␈↓␈αis␈αadded␈α
to␈αthe␈αproperty␈α
list,␈αand␈α
␈↓α0␈↓␈αis␈αplaced␈α
in␈αthe␈α␈↓αcdr␈↓-part␈α
of
␈↓loc␈α
and␈α
the␈α
partial␈α
instruction␈α
is␈α
placed␈α
in␈α
the␈α
␈↓αcar␈↓-part.␈α
Thus:






␈↓Any␈αsucceeding␈αreferences␈αto␈α
␈↓αn␈↓␈αresult␈αin␈αchanging␈αthe␈α
␈↓αcdr␈↓␈αof␈αthe␈αp-list␈αpair,␈α
call␈αit␈αv,␈αto␈αpoint␈α
to␈αa
␈↓cell␈α
whose␈α
␈↓αcar␈↓-part␈α
is␈α
loc␈αand␈α
whose␈α
␈↓αcdr␈↓-part␈α
is␈α
v.␈α
 As␈αabove␈α
we␈α
deposit␈α
the␈α
partial␈α
instruction␈αin
␈↓the␈α
␈↓αcar␈↓-part␈α
and␈α
␈↓α0␈↓␈α
in␈α
the␈α
␈↓αcdr␈↓␈α
part␈α
of␈α
loc.␈α
 Thus␈α
the␈α
next␈α
reference␈α
would␈α
update␈α
our␈α
example␈α
to:
␈↓␈↓↓8.5␈↓ π6Applications of ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓     173␈↓α













␈↓When␈αthe␈αlabel␈α␈↓αn␈↓␈αfinally␈αis␈αdefined␈αwe␈αmust␈αperform␈αthe␈αfixups,␈αdelete␈αthe␈α␈↓αUNDEF␈↓␈αpair,␈αand␈αadd
␈↓a␈α
pair␈α
␈↓α(SYM␈↓␈α
.␈α
loc␈α
␈↓α)␈↓␈α
on␈α
the␈α
p-list.











␈↓Here␈αare␈α
the␈αfunctions.␈α
␈↓αdefloc␈↓␈αis␈α
called␈αwhen␈α
a␈αlabel␈α
has␈αbeen␈α
defined;␈α␈↓αgval␈↓␈α
is␈αcalled␈α
when␈αa␈αlabel␈α
is
␈↓referenced.
␈↓αdefloc <= λ[[lab;loc]prog␈↓ ∧H[[z]
␈↓α␈↓ ∧H␈↓ ¬8[z ← get[lab;UNDEF] → go[fixup]
␈↓α␈↓ ∧Ha␈↓ ¬8return[putprop[lab;loc;SYM]]
␈↓α␈↓ ∧Hfixup␈↓ ¬8[null[z] → rplacd[lab;cdddr[lab]];go[a]]
␈↓α␈↓ ∧H␈↓ ¬8deposit[car[z];plus[examine[car[z]];loc]]
␈↓α␈↓ ∧H␈↓ ¬8z ← cdr[z];
␈↓α␈↓ ∧H␈↓ ¬8go[fixup] ]]
␈↓αgval <= λ[[lab]␈↓ β([get[lab;SYM];
␈↓α␈↓ β( T → putprop[lab;cons[loc;get[sym;UNDEF]];UNDEF]0]]

␈↓Notes:␈α
␈↓αgval␈↓␈α
is␈α
full␈α
of␈α
pitfalls.

␈↓␈↓↓1␈↓.␈α
 ␈↓αloc␈↓␈α
is␈α
a␈α
global␈α
variable.

␈↓␈↓↓2␈↓.␈α
 There␈α
is␈αno␈α
e␈↓β1␈↓;␈α
we␈αassume␈α
that␈α
if␈αp␈↓β1␈↓␈α
evaluates␈α
to␈α
something␈αnot␈α
equal␈α
to␈α␈↓αNIL␈↓,␈α
then␈α
that␈αvalue␈α
is
␈↓the␈α
value␈α
of␈α
the␈α
conditional␈α
expression.

␈↓␈↓↓3␈↓.␈α
 Otherwise␈α
the␈α
␈↓αputprop␈↓␈α
is␈α
executed␈α
and␈α
␈↓α0␈↓␈α
is␈α
returned.
␈↓␈↓↓174  Storage structures and efficiency␈↓ 38.5␈↓


␈↓See␈α∂problem␈α⊂III,␈α∂page␈α∂154,␈α⊂and␈α∂problem␈α∂***␈α⊂at␈α∂the␈α∂end␈α⊂of␈α∂this␈α∂section␈α⊂for␈α∂the␈α∂description␈α⊂of␈α∂a
␈↓complete␈α
assembler␈α
for␈α
our␈α
latest␈α
␈↓αcompile␈↓.

␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓assembler

␈↓II.␈α
More␈α
on␈α
␈↓αratom␈↓.

␈↓Recall␈α
the␈αdiscussion␈α
of␈α␈↓αratom␈↓␈α
in␈α
Section␈α6.4␈α
and␈αSection␈α
6.5.␈α
 Now␈αthat␈α
you␈αknow␈α
about␈α␈↓αrplaca␈↓␈α
and
␈↓␈↓αrplacd␈↓␈α
write␈α
a␈α
more␈α
detailed␈α
version␈α
of␈α
␈↓αratom␈↓.

␈↓III.␈αRecall␈αthe␈αfunction␈αwhich␈αreverses␈α
the␈αtop-level␈αelements␈αof␈αa␈αlist.␈α
 On␈αpage␈α21␈αand␈αpage␈α22␈α
we
␈↓wrote␈αit␈αin␈α
various␈αstyles␈αand␈αwith␈α
varying␈αdegrees␈αof␈αefficiency.␈α
 All␈αof␈αthese␈αfunctions␈α
used␈α␈↓αcons␈↓;
␈↓however␈αit␈αis␈αclear␈αthat␈αwe␈αshould␈αbe␈αable␈αto␈αreverse␈αa␈αlist␈αwithout␈αusing␈α␈↓↓any␈↓␈αnew␈αcells.␈α Write␈αan
␈↓AMBIT␈αalgorithm␈α
for␈αsuch␈α
a␈αreversing␈α
function.␈αExpress␈α
this␈αalgorithm␈α
as␈αa␈α
LISP␈αfunction.␈αIf␈α
you
␈↓use␈α
␈↓αprog␈↓␈α
don't␈α
use␈α
any␈α
␈↓αprog␈↓-variables.
␈↓␈↓↓9.␈↓ λRSystems Organization     175␈↓


␈↓␈↓ ¬␈␈↓↓SECTION 9

␈↓↓␈↓ ¬λSYSTEMS ORGANIZATION␈↓




␈↓There␈α
are␈αmany␈α
applications␈αof␈α
data␈αstructures␈α
which␈αarise␈α
very␈αnaturally␈α
in␈αthe␈α
domain␈αof␈α
systems
␈↓programming.␈α↔ This␈α↔section␈α↔will␈α⊗begin␈α↔with␈α↔a␈α↔very␈α⊗brief␈α↔historical␈α↔description␈α↔of␈α⊗systems
␈↓organization,␈αfrom␈αthe␈αbare␈αmachine␈αto␈αmulti-processing.␈α In␈αthe␈αearly␈αdays␈αof␈αcomputers,␈αoperating
␈↓systems␈αwere␈αnon-existent.␈α You␈αwould␈αsign␈αup␈αfor␈αa␈αcouple␈αof␈αhours␈αof␈αmachine␈αtime,␈αappear␈αwith
␈↓your␈α
card␈α
deck␈α
or␈α
paper␈α
tape,␈α
and␈α
operate␈α
the␈α
machine.␈α
 Debugging␈α
devices␈α
consisted␈α
of␈α
a␈α
sharp
␈↓pointed␈α∞stick,␈α∞and␈α
a␈α∞quick␈α∞hand␈α∞on␈α
the␈α∞console␈α∞switches.␈α∞ This␈α
means␈α∞of␈α∞programming␈α∞was␈α
very
␈↓satifying␈αto␈αmany␈αof␈αthe␈αprogrammers,␈αbut␈αmachine␈αutilization␈αleft␈αsomething␈αto␈αbe␈αdesired.␈α Much
␈↓of␈α
the␈α
time␈α
the␈α
machine␈α
would␈αsitt␈α
idle␈α
(stopped)␈α
as␈α
you␈α
would␈αthink␈α
about␈α
what␈α
to␈α
do␈α
next.␈α As
␈↓processing␈αspeed␈αand␈αmachine␈αcosts␈αincreased␈αit␈α
became␈αevident␈αthat␈αthis␈αmode␈αof␈αoperation␈αhad␈α
to
␈↓go.␈α∞ The␈α∞first␈α∞operating␈α
systems␈α∞consisted␈α∞of␈α∞a␈α
dull␈α∞slow␈α∞object␈α∞called␈α
an␈α∞operator␈α∞and␈α∞a␈α
satellite
␈↓computer␈αon␈α
which␈αan␈αinput␈α
tape␈αconsisting␈α
of␈αmany␈αseparate␈α
jobs␈αwas␈αcreated.␈α
 Each␈αjob␈α
on␈αthe
␈↓input␈α∂tape␈α∂was␈α∞swquentially␈α∂read␈α∂into␈α∂memory,␈α∞executed␈α∂and␈α∂the␈α∞output␈α∂presented␈α∂to␈α∂an␈α∞output
␈↓tape.␈α If␈α
some␈αabnormal␈α
behavior␈αwas␈α
detected␈αin␈α
your␈αprogram,␈α
you␈αwere␈α
also␈αpresented␈α
with␈αan
␈↓often␈α
uninspiring␈α
octal␈αdump␈α
of␈α
the␈αcontents␈α
of␈α
memory.␈αWhenever␈α
a␈α
job␈αrequired␈α
say,␈α
a␈αdata␈α
tape,
␈↓the␈α
operator␈α
would␈αbe␈α
notified,␈α
and␈α
the␈αmachine␈α
would␈α
wait␈α
until␈αthe␈α
tape␈α
was␈α
located,␈αmounted,
␈↓and␈α∞readied.␈α∞ There␈α∞was␈α∞a␈α∞moderately␈α∞small␈α∞resident␈α∞monitor␈α∞which␈α∞controlled␈α∞the␈α∂input,␈α∞output
␈↓and␈α∞perhaps␈α
the␈α∞timing␈α
of␈α∞jobs.␈α
 Gross␈α∞utilization␈α
of␈α∞the␈α
machine␈α∞was␈α
increased,␈α∞however␈α∞at␈α
the
␈↓cost␈α
of␈α
easy␈α
debugging.␈α
 This␈α
mode␈α
of␈α
operation␈α
is␈α
called␈α
batch␈α
processing.

␈↓The␈α
CPU␈α
(central␈α
processing␈α
unit)␈α
still␈α
locks␈α∞up␈α
when␈α
I/O␈α
devices␈α
aren't␈α
ready␈α
and␈α
the␈α∞user␈α
can
␈↓physically␈αhalt␈αthe␈αmachine.␈α For␈αthis␈αmodel␈αand␈αmost␈αof␈αthe␈αfuture␈αdiscussion,␈αit␈αsimplifies␈αmatters
␈↓to␈α
assume␈α
that␈α
the␈α
monitor␈α
resides␈α
in␈α
a␈α
separate␈α
core␈α
box␈α
from␈α
that␈α
which␈α
contains␈α
user␈α
programs.

␈↓Thus:











␈↓The␈αuser␈αprograms␈αare␈αloaded␈αinto␈αexplicit␈α(hard)␈αlocations␈αin␈αmemory,␈αand␈αit␈αis␈αassumed␈αthat␈αthe
␈↓user␈α
has␈α
access␈α
to␈α
all␈α
of␈α
the␈α
addressing␈α
space␈α
of␈α
his␈α
core␈α
box.
␈↓␈↓↓176  Systems Organization␈↓ C9.␈↓


␈↓In␈α
an␈α
attempt␈α∞to␈α
decrease␈α
the␈α
overhead␈α∞on␈α
waiting␈α
for␈α
external␈α∞actions␈α
(I/O␈α
requests␈α∞or␈α
operator
␈↓intervention)␈α
we␈α
attach␈α
a␈α
reasonably␈α
fast␈α
storage␈α
device␈α
(and␈α
increase␈α
the␈α
size␈α
of␈α
the␈α
monitor).␈α
 This
␈↓storage␈αdevice␈αis␈αcapable␈αof␈αholding␈αmany␈α
user␈αjobs.␈α Whenever␈αa␈αuser␈α(loser)␈αprogram␈α
requires␈αa
␈↓service␈α
which␈α
cannot␈α
be␈α
satisfied␈α
immediately,␈α
the␈αmonitor␈α
will␈α
swap␈α
the␈α
program␈α
out␈α
of␈αmemory
␈↓onto␈α
this␈αstorage␈α
device,␈α
initiate␈αaction␈α
which␈αwill␈α
tend␈α
to␈αsatisfy␈α
the␈α
request,␈αand␈α
bring␈αanother␈α
job
␈↓into␈α
core,␈α
beginning␈α
its␈α
execution.␈α This␈α
new␈α
job␈α
may␈α
either␈α
be␈αthe␈α
next␈α
job␈α
on␈α
the␈α
input␈αstream,␈α
or
␈↓a␈α
job␈αwhich␈α
was␈α
swapped␈αout␈α
and␈α
now␈αis␈α
ready␈αto␈α
run.␈α
The␈αuser␈α
still␈α
is␈αgiven␈α
the␈αwhole␈α
addressing
␈↓space,␈αhe␈αis␈αstill␈αloaded␈αinto␈αexplicit␈αmemory␈αlocations,␈αbut␈αthe␈αmonitor␈αmust␈αnow␈αbe␈αmore␈αcleaver.
␈↓It␈αmust␈αbe␈αable␈αto␈αrecognize␈α(or␈α`trap')␈αrequests␈αwhich␈αwould␈αlock␈αup␈αthe␈αCPU.␈α It␈αmust␈αbe␈αable␈αto
␈↓make␈α
decisions␈α
as␈α
to␈α
which␈α
job␈α
to␈α
run␈α
next.











␈↓Clearly␈αthere␈α
are␈αgrossinefficiencies␈α
in␈αthe␈α
present␈αscheme.␈α
 Allowing,␈αor␈α
in␈αfact␈α
requiring,␈αthat␈α
each
␈↓user␈αhave␈αaccess␈αto␈αall␈αof␈αmemory␈αis␈αtoo␈αgenerous.␈α Assume␈αthat␈αour␈αmemory␈αsize␈αis␈α32→␈αwords␈αand
␈↓assume␈α
that␈α
we␈α
have␈α16K␈α
jobs␈α
which␈α
are␈α
ready␈αto␈α
run.␈α
 We␈α
should␈α
be␈αable␈α
to␈α
load␈α
one␈α
job␈αinto
␈↓locations␈α
0␈α-␈α
(16K-1)␈α
and␈αload␈α
the␈α
other␈αjob␈α
into␈α
the␈αother␈α
half␈α
of␈αmemory.␈α
 When␈α
one␈αjob␈α
requires
␈↓external␈α
service,␈αwe␈α
should␈α
be␈αable␈α
to␈α
switch␈αthe␈α
CPU␈α
to␈αbegin␈α
execution␈α
of␈αthe␈α
other␈αjob␈α
provided
␈↓that␈α∞we␈α∞save␈α∂all␈α∞information␈α∞about␈α∂the␈α∞current␈α∞state␈α∞of␈α∂the␈α∞suspended␈α∞jog.␈α∂ The␈α∞point␈α∞is␈α∂that␈α∞it
␈↓takes␈α
time␈αto␈α
swap␈αjobs␈α
in␈α
and␈αout␈α
of␈αcore␈α
so␈α
thaat␈αanything␈α
we␈αcan␈α
do␈α
to␈αdecrease␈α
this␈αoverhead␈α
is
␈↓a␈αwinner.␈α What␈αare␈αthe␈αadded␈αrequirements␈αfor␈αthis␈αscheme?␈α Manily␈αwe␈αmust␈αhave␈αsome␈αscheme
␈↓for␈αkeeping␈αthe␈αjobs␈αout␈αof␈αeach␈αother's␈αhair.␈α This␈αis␈αusually␈αdone␈αby␈αa␈αhardware␈αaddition␈αto␈αthe
␈↓machine␈α⊃called␈α⊃the␈α⊃protection␈α⊃register.␈α⊃ In␈α⊂this␈α⊃simple␈α⊃scheme␈α⊃the␈α⊃protection␈α⊃register␈α⊃would␈α⊂be
␈↓loaded␈αby␈αthe␈αmonitor␈αwith␈αthe␈αupper␈αand␈αlower␈αbounds␈αon␈αthe␈αaddressing␈αspace␈αaccessible␈αto␈αthe
␈↓current␈α
job.␈α
 Then␈αevery␈α
memory␈α
reference␈α
made␈αby␈α
a␈α
program␈αis␈α
filtered␈α
through␈α
the␈αprotection
␈↓register.␈α
 Obviously␈αthe␈α
instruction␈α
to␈αload␈α
protection␈αregister␈α
should␈α
be␈αrestricted␈α
to␈α
execution␈αby
␈↓the␈α
monitor.

␈↓What␈αare␈αthe␈αinefficiencies␈αin␈αthis␈αscheme?␈α Consider␈αwhat␈αhappens␈αwhen␈αwe␈αhave␈αto␈αswap␈α
a␈αjob
␈↓out␈α
of␈α
memory.␈α
 Since␈α
the␈αaddresses␈α
used␈α
by␈α
the␈α
program␈αare␈α
explicitly␈α
tied␈α
to␈α
memory␈αlocations,␈α
we
␈↓must␈αswap␈αthat␈αjob␈αback␈αinto␈αexacly␈αthe␈αsame␈αlocations␈αif␈αit␈αis␈αto␈αrun␈αcorrectly.␈α Consider␈αtwo␈α16K
␈↓programs␈αwhose␈αonly␈αeffect␈αis␈αto␈αexecute␈α`jump-self'␈αloops␈α(␈αL␈α(JRST␈αL)␈α).␈αTheir␈αinitial␈αload␈αmight
␈↓look␈α
like:
␈↓␈↓↓9.␈↓ λRSystems Organization     177␈↓













␈↓If␈αwe␈αswap␈αout␈αthe␈αtop␈αjob␈αand␈αtry␈αto␈αswap␈αit␈αback␈αinto␈αthe␈αlower␈α16K␈αat␈αsome␈αlater␈αtime,␈αwe␈αlose
␈↓big.











␈↓But␈αclearly␈α
if␈αthe␈α
bottom␈α16K␈αis␈α
available␈αwe␈α
should␈αbe␈α
able␈αto␈αuse␈α
it.␈α We␈α
want␈αto␈α
be␈αable␈αto␈α
swap
␈↓our␈α
programs␈α
into␈α
any␈α
available␈α
block␈α
of␈α
core␈α
which␈α
is␈α
of␈α
sufficient␈α
size.

␈↓To␈αaccomplish␈αthis␈αwe␈αadd␈αa␈αnew␈αpiece␈αof␈αhardware,␈αthe␈αrelocation␈αregister.␈αNow␈αour␈αloader␈αloads
␈↓all␈α
our␈α
programs␈α
as␈α
if␈α
they␈α
begin␈α
at␈α
location,␈α
0.␈α
 Thus␈α
in␈α
the␈α
above␈α
example:











␈↓To␈α
get␈α
the␈α
appropriate␈α∞effect␈α
when␈α
we␈α
execute␈α
any␈α∞program,␈α
we␈α
bias␈α
every␈α
memory␈α∞reference␈α
by
␈↓actual␈α
address␈αassigned␈α
to␈α
the␈αprogram's␈α
location␈α
0.␈α This␈α
bias␈αis␈α
put␈α
in␈αthe␈α
relocation␈α
register␈αby
␈↓the␈αmonitor␈αbefore␈αit␈αbegins␈αexecution␈αof␈αthe␈αprogram.␈α With␈αthis␈αscheme␈αwe␈αcan␈αrun␈αany␈α
jobs␈αin
␈↓any␈α
block␈α
of␈α∞core␈α
which␈α
is␈α∞the␈α
correct␈α
size.␈α∞ All␈α
we␈α
need␈α∞do␈α
is␈α
load␈α∞the␈α
appropriate␈α
offset␈α∞in␈α
the
␈↓relocation␈α
register.
␈↓␈↓↓178  Systems Organization␈↓ C9.␈↓













␈↓Now␈αwe␈α
can␈αdispense␈α
with␈αthe␈α
two-core␈αbox␈α
approach.␈α Just␈α
use␈αone␈α
box␈αand␈α
bias␈αevery␈α
access␈αin␈α
a
␈↓user␈α
program␈α
by␈α
the␈α
length␈α
of␈α
the␈α
monitor␈α
plus␈α
the␈α
usual␈α
offset:











␈↓The␈αactual␈αharrdware␈αcan␈αalso␈αbe␈αsimplified␈αnow␈αsince␈αthe␈αlower␈αbound␈αon␈αevery␈αuser's␈αaddressing
␈↓space␈α
is␈α
always␈α
zero.

␈↓Clearly,␈α∞more␈α∞refinements␈α∞need␈α∞to␈α∞be␈α∞made.␈α∞ At␈α∞present␈α∞the␈α∞only␈α∞way␈α∞to␈α∞interrupt␈α∞a␈α∞job␈α∞is␈α∞if␈α
he
␈↓terminates,␈α⊃or␈α∩calls␈α⊃for␈α∩some␈α⊃external␈α∩action,␈α⊃or␈α⊃attempts␈α∩to␈α⊃perform␈α∩some␈α⊃illegal␈α∩or␈α⊃restricted
␈↓instruction.␈α Any␈αoperating␈αsystem␈αneeds␈αto␈αbe␈αable␈α
to␈αmonitor␈αthe␈αamount␈αof␈αCPU␈αtime␈αspent␈αon␈α
a
␈↓job.␈α
 So␈α∞we␈α
should␈α
have␈α∞a␈α
programmable␈α
clock␈α∞which␈α
can␈α
be␈α∞set␈α
by␈α
the␈α∞monitor␈α
and␈α∞which␈α
will
␈↓send␈α∞an␈α∞interrupt␈α∞to␈α∞the␈α∞at␈α∞a␈α∞specified␈α∞time.␈α∞ Actually␈α∞other␈α∞hardware␈α∞needs␈α∞to␈α∞be␈α∞added␈α∞to␈α∞our
␈↓entourage␈αif␈α
we␈αintend␈αto␈α
do␈αreasonable␈αtime-sharing.␈α
 A␈αsophisticated␈αpriority␈α
interrupt␈αsystem␈αis␈α
a
␈↓necessity.␈α Since␈αmany␈αof␈αthe␈αapplications␈αof␈αdata␈αstructures␈αappear␈αin␈αtime-sharing␈αsystems,␈αwe␈α
will
␈↓say␈α
a␈α
bit␈α
about␈α
the␈α
behavior␈α
of␈α
such␈α
systems.



␈↓␈↓ ¬↓␈↓↓9.1  Time-sharing organization␈↓


␈↓What␈α∂make␈α∂time-sharing␈α∂viable␈α∂is␈α∂the␈α∂tremendous␈α∂difference␈α∂between␈α∂human␈α∂reaction␈α∂time␈α∞and
␈↓machine␈αspeeds.␈α In␈αa␈αperiod␈αof␈αa␈αfew␈α
seconds␈αa␈αwell␈αdesigned␈αt-s␈αsystem␈αcan␈αsatisfy␈αsimple␈α
requests
␈↓by␈α
many␈α
users.␈α
 If␈α
a␈α
user␈α
must␈α
wait␈α
a␈α∞significant␈α
length␈α
of␈α
time␈α
to␈α
obtain␈α
a␈α
response␈α
to␈α∞a␈α
simple
␈↓command,␈α∞then␈α∞your␈α∂system␈α∞is␈α∞in␈α∞trouble.␈α∂ The␈α∞heart␈α∞of␈α∞a␈α∂rapid␈α∞response␈α∞is␈α∞a␈α∂priority␈α∞interrupt
␈↓system.
␈↓␈↓↓9.1␈↓ λ⊃Time-sharing organization     179␈↓


␈↓A␈α∩time-sharing␈α∩monitor␈α⊃must␈α∩service␈α∩terminals,␈α⊃mass␈α∩storage␈α∩devices␈α⊃and␈α∩control␈α∩the␈α⊃dynamic
␈↓allocation␈αof␈αits␈αmemories.␈α Consider␈αthe␈αcare␈αand␈αfeeding␈αof␈αa␈αrelatively␈αfast␈αstorage␈αdevice,␈αsay␈αa
␈↓disk,␈α∞and␈α∂its␈α∞interaction␈α∂with␈α∞the␈α∂sending␈α∞and␈α∞receiving␈α∂of␈α∞characters␈α∂from␈α∞user␈α∂terminals.␈α∞Most
␈↓disks␈α∂require␈α∂a␈α⊂sequence␈α∂of␈α∂commands␈α∂be␈α⊂issued␈α∂before␈α∂an␈α∂actual␈α⊂data␈α∂transfer␈α∂can␈α⊂take␈α∂place.
␈↓Assume␈αthat␈αthere␈α
are␈αtwo␈αcommands:␈α
a␈αseek␈αto␈α
find␈αthe␈αappropriate␈α
area␈αon␈αthe␈α
device,␈αfollowed
␈↓by␈αthe␈αtransfer␈αcommand.␈α If␈αthe␈αCPU␈αwere␈αtied␈αup␈αfrom␈αthe␈αbeginning␈αof␈αthe␈αseek␈αto␈αthe␈α
end␈αof
␈↓the␈αtransfer,␈αsignificant␈αamounts␈αof␈αCPU␈αtime␈αwould␈αbe␈αlost.␈α If␈αwe␈αissued␈αthe␈αseek,␈αwent␈αoff␈αto␈αdo
␈↓other␈αcalculations,␈αbut␈α
were␈αnot␈αresponsive␈αenough␈α
when␈αthe␈αseek␈αwas␈α
completed␈αwe␈αmight␈αmiss␈α
the
␈↓chance␈α
to␈α
make␈α
the␈α
transfer␈α
due␈α
to␈α
intervening␈α∞rotation␈α
of␈α
the␈α
disk.␈α
 What␈α
we␈α
can␈α
do␈α
is␈α∞put␈α
the
␈↓disk␈α∩on␈α∩a␈α∩high␈α∩priority␈α∩interrupt␈α∩channel␈α⊃and␈α∩the␈α∩terminal␈α∩keyboards␈α∩on␈α∩a␈α∩medium␈α⊃priority
␈↓channel.␈α
 Issue␈α∞the␈α
seek,␈α
go␈α∞back␈α
to␈α
computation,␈α∞perhaps␈α
servicing␈α
the␈α∞keyboards;␈α
and␈α∞when␈α
the
␈↓disk␈α
is␈α
in␈α
position␈α
we␈αwill␈α
get␈α
a␈α
high␈α
priority␈α
interrupt,␈αfreezing␈α
the␈α
state␈α
of␈α
the␈α
computation;␈αat␈α
this
␈↓time,␈α
begin␈α∞the␈α
transfer␈α
of␈α∞information,␈α
dismissing␈α
the␈α∞interrupt␈α
and␈α
continuing␈α∞the␈α
computation.
␈↓Thus␈α∪higher␈α∪priority␈α∪requests␈α∪can␈α∪interrupt␈α∪lower␈α∪ones;␈α∪any␈α∪lower␈α∪priority␈α∪requests␈α∪must␈α∩be
␈↓suspended␈α
or␈αsaved␈α
until␈α
the␈αhigher␈α
one␈αis␈α
completed.␈α
 You␈αmight␈α
decide␈α
to␈αdesign␈α
the␈αhardware␈α
to
␈↓allow␈α⊃recursive␈α⊃interrupts␈α⊂on␈α⊃a␈α⊃particular␈α⊂channel␈α⊃or␈α⊃might␈α⊂require␈α⊃completion␈α⊃before␈α⊂another
␈↓request␈α
can␈α
be␈α
serviced.

␈↓What␈αabout␈αthe␈αdata␈αstructures␈αused␈αin␈αa␈αcomplex␈αsystem.␈α Data␈αstructures␈αare␈αused␈αheavily␈αin␈αthe
␈↓scheduling␈αalgorithms.␈α the␈αt-s␈αmonitor␈αmust␈αmake␈αdecisions␈αabout␈αwhich␈αjobs␈αin␈αthe␈αsystem␈αare␈αto
␈↓run.␈α These␈αdecisions␈αare␈αbased␈αon␈αsuch␈αsimple␈αthings␈αas:␈αthe␈αjob␈αisn't␈αrequesting␈αservice--dormant;
␈↓the␈α∂job␈α∂is␈α∂waiting␈α∂for␈α∂some␈α∂I/O␈α∂device--I/O␈α∂wait;␈α∂or␈α∂decisions␈α∂must␈α∂be␈α∂based␈α∂on␈α∂more␈α∂difficult
␈↓criteria:␈α
the␈α
job␈α
is␈α
ready␈α
to␈α
run␈α
but␈α
is␈α
too␈α
big␈α
for␈α
the␈α
currently␈α
available␈α
allotment␈α
of␈α
core;␈α
there␈αis␈α
a
␈↓job␈α
ready␈α
which␈α
needs␈α
fast␈α
response␈α
or␈α
has␈α
been␈α
waiting␈α
inordinately␈α
long␈α
for␈α
service.

␈↓In␈α∞general␈α∞a␈α∞vast␈α
array␈α∞of␈α∞information␈α∞can␈α
be␈α∞used␈α∞in␈α∞deciding␈α
which␈α∞job␈α∞to␈α∞run␈α∞next.␈α
 Usually
␈↓these␈αdecisions␈αtake␈αshape␈αby␈αreqquiring␈αthat␈αthe␈αjobs␈αcurrently␈αin␈αthe␈αsystem␈αbe␈αpartitioned␈αinto␈αa
␈↓finite␈α∞set␈α∂of␈α∞waiting-lines␈α∞or␈α∂queues.␈α∞ The␈α∂scheduling␈α∞algorithm␈α∞manipulates␈α∂these␈α∞queues␈α∂as␈α∞the
␈↓status␈αof␈αthe␈αjobs␈αchange.␈α A␈αqueue␈αis␈α
a␈αdata␈αstructure.␈α Queues␈αare␈αalso␈αcalled␈αfirst-in␈αfirst-out␈α
lists
␈↓(FIFO)␈α∞or␈α
pop-up␈α∞lists.␈α
 In␈α∞LISP␈α
parlance␈α∞you␈α
append␈α∞new␈α
queue-elements␈α∞to␈α
the␈α∞end␈α
of␈α∞the␈α
list
␈↓and␈α
take␈αthe␈α
elements␈α
off␈αof␈α
the␈α
front␈αof␈α
the␈α
list.␈α It␈α
should␈α
be␈αobvious␈α
where␈α
the␈αname␈α
waiting-line
␈↓comes␈α
from.

␈↓Queues␈α∂are␈α∞also␈α∂used␈α∞in␈α∂the␈α∂buffering␈α∞schemes␈α∂of␈α∞I/O␈α∂devices.␈α∂ As␈α∞we␈α∂type␈α∞`LOGIN.....'␈α∂to␈α∂a␈α∞t-s
␈↓system,␈αit␈αis␈αusually␈αthe␈αcase␈αthat␈αthe␈αcharacter␈αstring␈αfirst␈αgoes␈αinto␈αa␈αsystem␈αbuffer␈αand␈αwhen␈αthe
␈↓CPU␈αis␈αfree,␈αthe␈αcommand␈αis␈αdecoded.␈α Obviously␈αwe␈αwant␈αthe␈αmonitor␈αto␈αsee␈αthe␈αcharacter␈αstring
␈↓in␈α
the␈α
same␈α
order␈α
in␈α
which␈α
we␈α
typed␈α
it.␈α
 That␈α
is␈α
the␈α
buffer␈α
is␈α
acting␈α
like␈α
a␈α
queue.

␈↓A␈αqueue␈αcan␈αbe␈αimplemented␈αas␈αsimply␈αa␈αlist␈αwith␈αa␈αpointer␈αto␈αthe␈αfront,␈αand␈αa␈αpointer␈αto␈αthe␈αend.
␈↓When␈α∞you␈α∞add␈α∞something␈α∞to␈α∂the␈α∞end,␈α∞you␈α∞update␈α∞one␈α∞pointer;␈α∂wen␈α∞you␈α∞take␈α∞an␈α∞element␈α∂off␈α∞the
␈↓front␈α∩you␈α∩update␈α∩the␈α∩other␈α∩pointer.␈α∩ There␈α∩is␈α⊃the␈α∩obvious␈α∩check␈α∩to␈α∩make␈α∩sure␈α∩that␈α∩you␈α⊃can
␈↓recognize␈α
the␈α
empty␈α
queue:␈α
when␈α
the␈α
front␈α
pointer␈α
meets␈α
the␈α
end␈α
pointer:
␈↓␈↓↓180  Systems Organization␈↓ 79.1␈↓









␈↓As␈α
with␈αstacks,␈α
the␈αqueue␈α
is␈αusually␈α
not␈αrepresented␈α
as␈αa␈α
list␈αof␈α
argitrary␈αlength.␈α
 A␈αqueue␈α
can␈αbe
␈↓represented␈α
as␈α
a␈α
sequential␈α
set␈α
of␈α
memory␈α
locations,␈α
still␈α
with␈α
two␈α
pointers.␈α
 What␈α
happens␈αwhen␈α
we
␈↓have␈α
filled␈αthe␈α
last␈αcell␈α
in␈α
the␈αsequence.␈α
 Well,␈αif␈α
some␈α
process␈αhas␈α
been␈αremoving␈α
items␈α
from␈αthe
␈↓queue,␈α
then␈α
the␈α
front␈α
pointer␈α
has␈α
moved:







␈↓In␈αthis␈αcase␈αthe␈αcells␈αfrom␈αthe␈αfirst␈αcell␈αin␈αthe␈αsequence␈αto␈αthe␈αcurrent␈αposition␈αof␈αthe␈αfront␈αpointer
␈↓are␈α
available.␈α
 Use␈α
`em.␈α
 That␈αis␈α
a␈α
queue␈α
is␈α
usually␈αimplemented␈α
as␈α
a␈α
sequential␈α
circular␈α
list␈αwith
␈↓two␈αpointers.␈α In␈αthis␈αimplementation␈αthe␈αtests␈α
for␈αthe␈αempty␈αqueue␈αare␈αslightly␈αmore␈α
complex,␈αbut
␈↓are␈α
still␈α
obvious.␈α
 Notice␈α
too,␈α
that␈α
we␈α
must␈α
now␈α
also␈α
test␈α
for␈α
the␈α
full␈α
queue.

␈↓In␈αsystem␈αapplications␈αit␈αis␈αusually␈αthe␈αcase␈αthat␈αone␈αprocess␈αis␈αfilling␈αthe␈αqueue␈αand␈αone␈αprocess␈αis
␈↓emptying␈α∞it.␈α∂ In␈α∞this␈α∂application,␈α∞a␈α∂full␈α∞queue␈α∞should␈α∂simply␈α∞put␈α∂the␈α∞filling␈α∂process␈α∞to␈α∂`sleep'␈α∞(or
␈↓suspend␈αit),␈αand␈αwhen␈αa␈αqueue␈αbecomes␈αempty,␈αthe␈αemptying␈αprocess␈αshould␈αbe␈α
suspended.␈α There
␈↓are␈α∞some␈α∞interesting␈α∞and␈α∞non-trivial␈α∞problems␈α
which␈α∞arise␈α∞in␈α∞connection␈α∞which␈α∞such␈α
`cooperating
␈↓processes'.

␈↓There␈αare␈αsome␈αinteresting␈αdata␈αstructure␈αapplications␈αwhich␈αare␈αconnected␈αwith␈αthe␈αallocationa␈αdn
␈↓(?)␈αliberation␈αof␈αstorage.␈αThe␈αmonitor␈αmust␈αbe␈αable␈αto␈αallocate␈αstorage␈αto␈αjobs␈αas␈αtheir␈αrequirements
␈↓become␈αknown␈αand␈αmust␈αalso␈αbe␈αable␈αto␈αrecover␈αareas␈αof␈αmemory␈αwhich␈αare␈αno␈αlonger␈αbeing␈αused.
␈↓Similarly,␈α
since␈α∞an␈α
integral␈α
part␈α∞of␈α
a␈α
large␈α∞system␈α
is␈α
a␈α∞file␈α
structure,␈α
the␈α∞monitor␈α
must␈α
be␈α∞able␈α
to
␈↓handle␈α∞the␈α∞demands␈α∞of␈α∞file␈α∞maintenance.␈α∂ These␈α∞two␈α∞problems␈α∞are␈α∞related␈α∞but␈α∞solutions␈α∂for␈α∞core
␈↓allocation␈α
are␈α
not␈α
necessarily␈α
reasonable␈α
ones␈α
for␈α
file␈α
allocation.
␈↓␈↓↓10.␈↓ π=Implications for other languages     181␈↓


␈↓␈↓ ¬y␈↓↓SECTION 10

␈↓↓␈↓ ∧⊗IMPLICATIONS FOR OTHER LANGUAGES␈↓




␈↓**development␈α
of␈α
s-LISP**

␈↓compilers␈α
for␈α
other␈α
langauages␈α
static␈α
and␈α
dynamic␈α
linking

␈↓parsing␈α
and␈α
scanning␈α
and␈α
sym␈α
tabs

␈↓show␈α
how␈α
easy␈α
it␈α
is␈α
now.

␈↓extensions:␈α
PLANNER␈α
CONNIVER,␈α
data␈α
bases␈α
and␈α
pattern␈α
matching␈α
?EL1?

␈↓simulation␈α
languages␈α
==>␈α
funargs␈α
==>␈α
ACTORS
␈↓␈↓↓182  BIBLIOGRPAHY␈↓ 911.␈↓


␈↓␈↓ ¬z␈↓↓SECTION 11

␈↓↓␈↓ ¬XBIBLIOGRAPHY␈↓



␈↓Wegner, P.␈↓ ∧HThree Computer Cultures
␈↓␈↓ ∧HProgramming languages, information structures..(book)
␈↓␈↓ ∧HSemantics of programming languages

␈↓Gries, D.␈↓ ∧HCompiler Construction
␈↓Gries and Feldman␈↓ ∧HTranslator Writing Systems

␈↓McCarthy, J.␈↓ ∧HRecursive Functions..(CACM)
␈↓␈↓ ∧HTowards a mathemtical ...
␈↓␈↓ ∧HMicro algol
␈↓␈↓ ∧HLISP 1.5 programmer's manual

␈↓Wegbreit, B.␈↓ ∧HThesis on Extensible languages
␈↓Wegbreit and Bobrow␈↓ ∧HBBN report on control structures

␈↓Johnston, J.␈↓ ∧HContour Model

␈↓Berry, D.␈↓ ∧HRetention or deletion?
␈↓␈↓ ∧HOREGANO

␈↓London, R.␈↓ ∧HCorrectness of two compilers...

␈↓Wirth and Weber␈↓ ∧HEULER

␈↓Organick, E.␈↓ ∧HB6700

␈↓Weismann, C.␈↓ ∧HLISP 1.5 primer

␈↓Hewitt, C.␈↓ ∧HPLANNER

␈↓Sussman, G.␈↓ ∧HCONNIVER

␈↓Scott, D.␈↓ ∧HPrinceton paper on semantics

␈↓Hammer, M.␈↓ ∧HFormal Definition of BASEL

␈↓Moore, J.␈↓ ∧HThesis of proving properties

␈↓Cheatham, T.␈↓ ∧HCompiler Construction
␈↓␈↓↓␈↓ 
∀INDEX     183␈↓


␈↓␈↓αλ␈↓-expressions   47                        ␈↓ εh␈↓␈↓αnull␈↓   16, 167
␈↓␈↓α( )␈↓   14                                  ␈↓ εh␈↓␈↓αOBLIST␈↓   99, 101
␈↓␈↓α=␈↓   167                                   ␈↓ εh␈↓␈↓αor␈↓   64
␈↓␈↓αand␈↓   64, 159                             ␈↓ εh␈↓␈↓αperiod␈↓   97
␈↓␈↓αappend␈↓   21                               ␈↓ εh␈↓␈↓αPNAME␈↓   90
␈↓␈↓αapply␈↓   51                                ␈↓ εh␈↓␈↓αprin0␈↓   98
␈↓␈↓αarray␈↓   165                               ␈↓ εh␈↓␈↓αprin1␈↓   96
␈↓␈↓αassemble␈↓   144                            ␈↓ εh␈↓␈↓αprint␈↓   73, 97, 101
␈↓␈↓αassoc␈↓   45                                ␈↓ εh␈↓␈↓αprog␈↓   66
␈↓␈↓αatom␈↓   10, 89, 107                        ␈↓ εh␈↓␈↓αprog␈↓-variables   67
␈↓␈↓αbind␈↓   124                                ␈↓ εh␈↓␈↓αputprop␈↓   171
␈↓␈↓αblank␈↓   97                                ␈↓ εh␈↓␈↓αQUOTE␈↓   44, 50, 68
␈↓␈↓αcar␈↓   8, 109                              ␈↓ εh␈↓␈↓αratom␈↓   96, 99, 100, 172
␈↓␈↓αcar-cdr-␈↓↓chains␈↓   9                        ␈↓ εh␈↓␈↓αread␈↓   73, 90, 97, 101
␈↓␈↓αcdr␈↓   8, 107, 109                         ␈↓ εh␈↓␈↓αread␈↓ macros   122
␈↓␈↓αchar␈↓   167                                ␈↓ εh␈↓␈↓αread1␈↓   97
␈↓␈↓αcompile␈↓   136                             ␈↓ εh␈↓␈↓αremprop␈↓   171
␈↓␈↓αconcat␈↓   166                              ␈↓ εh␈↓␈↓αrest␈↓   166
␈↓␈↓αCOND␈↓   44, 50                             ␈↓ εh␈↓␈↓αreturn␈↓   68
␈↓␈↓αcons␈↓   7, 94, 107                         ␈↓ εh␈↓␈↓αreverse␈↓   21
␈↓␈↓αdefine␈↓   74, 89                           ␈↓ εh␈↓␈↓αrpar␈↓   97
␈↓␈↓αdeposit␈↓   146                             ␈↓ εh␈↓␈↓αrplaca␈↓   168
␈↓␈↓αeq␈↓   10, 91, 107                          ␈↓ εh␈↓␈↓αrplacd␈↓   169
␈↓␈↓αequal␈↓   11                                ␈↓ εh␈↓␈↓αSET␈↓   68
␈↓␈↓αeval␈↓   50, 72, 75, 101, 122, 137, 138     ␈↓ εh␈↓␈↓αSETQ␈↓   69
␈↓␈↓αevalquote␈↓   73                            ␈↓ εh␈↓␈↓αsogm␈↓   49
␈↓␈↓αevcond␈↓   50                               ␈↓ εh␈↓␈↓αstore␈↓   166
␈↓␈↓αexamine␈↓   146                             ␈↓ εh␈↓␈↓αSUBR␈↓   88, 124
␈↓␈↓αEXPR␈↓   89, 124                            ␈↓ εh␈↓␈↓αT␈↓   10
␈↓␈↓αFEXPR␈↓   124                               ␈↓ εh␈↓␈↓αtgmoaf␈↓   37, 137, 138
␈↓␈↓αfirst␈↓   166                               ␈↓ εh␈↓␈↓αtgmoaf␈↓   82
␈↓␈↓αFSUBR␈↓   124                               ␈↓ εh␈↓␈↓αtgmoafr␈↓   37, 137, 138, 143
␈↓␈↓αFUNARG␈↓   62                               ␈↓ εh␈↓␈↓αunbind␈↓   124
␈↓␈↓αFUNCTION␈↓   62                             ␈↓ εh␈↓␈↓αVALUE␈↓   88
␈↓␈↓αgensym␈↓   141                              ␈↓ εh␈↓␈↓αVALUE␈↓-cell   103, 124, 156
␈↓␈↓αget␈↓   123                                 ␈↓ εh␈↓␈↓	SM␈↓   128
␈↓␈↓αgetl␈↓   123                                ␈↓ εh␈↓a-list   156
␈↓␈↓αgo␈↓   67                                   ␈↓ εh␈↓a-lists   45
␈↓␈↓αlabel␈↓   59                                ␈↓ εh␈↓access evnironment   58
␈↓␈↓αlist␈↓   16                                 ␈↓ εh␈↓AMBIT/G   103
␈↓␈↓αlpar␈↓   97                                 ␈↓ εh␈↓analytic syntax   133
␈↓␈↓αmapcar␈↓   61                               ␈↓ εh␈↓apply   51
␈↓␈↓αmaplist␈↓   61                              ␈↓ εh␈↓assembler   154
␈↓␈↓αNIL␈↓   10, 14, 93                          ␈↓ εh␈↓assemblers   137, 142
␈↓␈↓↓184  INDEX␈↓ X␈↓


␈↓assignment   67                           ␈↓ εh␈↓Free space   101
␈↓association lists   45                    ␈↓ εh␈↓free storage area   94
␈↓atomic symbols   4                        ␈↓ εh␈↓free variables   59
␈↓atoms   4                                 ␈↓ εh␈↓free-space   91
␈↓auxiliary function   20                   ␈↓ εh␈↓FS   91
␈↓base language   135                       ␈↓ εh␈↓FS pointer   101
␈↓Binary Program Space   137                ␈↓ εh␈↓full word space   91
␈↓binary trees   5                          ␈↓ εh␈↓Full word space   101
␈↓bind   42, 51, 66, 103, 124               ␈↓ εh␈↓function   47
␈↓bootstrapping   137                       ␈↓ εh␈↓functional argument   60
␈↓BPS   137, 144                            ␈↓ εh␈↓functional composition   8
␈↓bucket   99                               ␈↓ εh␈↓FWS   91
␈↓bull   132                                ␈↓ εh␈↓FWS pointer   101
␈↓call-by-name   41                         ␈↓ εh␈↓garbage collection   94, 109, 166, 167
␈↓call-by-value   41                        ␈↓ εh␈↓garbage collector   94, 101, 109, 165, 172
␈↓case  statement   70                      ␈↓ εh␈↓global variables   102, 155
␈↓case statement   65                       ␈↓ εh␈↓hash consing   96
␈↓closure   71                              ␈↓ εh␈↓hashing   98
␈↓code generators   137                     ␈↓ εh␈↓hashing function   99
␈↓compiler   103, 127, 136, 147             ␈↓ εh␈↓inside-out   41
␈↓computed function   71                    ␈↓ εh␈↓internal lambdas   48
␈↓concrete syntax   134                     ␈↓ εh␈↓Knuth's Kompendium   171
␈↓conditional expression   10, 50, 143      ␈↓ εh␈↓lambda list   47
␈↓CONNIVER   76                             ␈↓ εh␈↓lambda variables   47
␈↓Contour Model   103, 117                  ␈↓ εh␈↓left-hand values   70
␈↓control environment   58                  ␈↓ εh␈↓length   67
␈↓data structures   1                       ␈↓ εh␈↓linear LISP   166
␈↓deep binding   103, 130, 155              ␈↓ εh␈↓LISP machine   72, 101
␈↓definition by recursion   18              ␈↓ εh␈↓list structure   90
␈↓differentiation   25                      ␈↓ εh␈↓list-notation   14
␈↓dotted-pairs   4                          ␈↓ εh␈↓lists   14
␈↓dynamic binding   155                     ␈↓ εh␈↓literal atoms   4
␈↓EL1   135                                 ␈↓ εh␈↓local variable   67
␈↓empty list   14                           ␈↓ εh␈↓M-expressions   72
␈↓eval   50                                 ␈↓ εh␈↓macros   103, 119, 158
␈↓evaluation   41                           ␈↓ εh␈↓mapping functions   60
␈↓examples of ␈↓αeval␈↓   52                     ␈↓ εh␈↓marking phase   95
␈↓extensible language   135                 ␈↓ εh␈↓MICRO-PLANNER   76
␈↓false   10                                ␈↓ εh␈↓MUDDLE   76
␈↓fibonacci sequence   20                   ␈↓ εh␈↓numbers   4, 159
␈↓fix-up   145                              ␈↓ εh␈↓object list   99
␈↓fixup   172                               ␈↓ εh␈↓OBLIST   109
␈↓form   9, 47                              ␈↓ εh␈↓offset   149
␈↓forward reference   144                   ␈↓ εh␈↓operation code   143
␈↓␈↓↓␈↓ 
∀INDEX     185␈↓


␈↓p-list   89
␈↓p-name   90
␈↓parser   97, 99, 117
␈↓partial functions   8
␈↓PDP-10   103
␈↓PL/1   135
␈↓PNAME   99
␈↓precedence   78
␈↓predicates   10
␈↓print-name   90
␈↓property-list   88
␈↓pushdown-list   125
␈↓ratom   100
␈↓reference counter   95
␈↓S-expressions   4
␈↓S-exprs   4
␈↓scanner   97, 99
␈↓SDIO   82
␈↓semantics   132
␈↓shallow  binding   156
␈↓shallow binding   103, 126, 130
␈↓shell languages   135
␈↓special form   64, 68
␈↓special forms   49, 119, 158
␈↓special variables   156
␈↓stack   125
␈↓static chain   155
␈↓storage reclaimer   94
␈↓storage structures   1
␈↓stratification   85
␈↓string  space   166
␈↓string processor   166
␈↓string space pointer   167
␈↓sweep phase   95, 172
␈↓symbol tables   45, 102, 143
␈↓symbolic expressions   4
␈↓syntactic dominoes   108
␈↓syntax-directed   78, 155
␈↓table-driven   68, 154
␈↓termination conditions   18
␈↓threading   115
␈↓total function   8
␈↓true   10
␈↓undefined   8, 91